def check_answer(x, y, op, result, user_choice): if user_choice == True: if result == eval(x, y, op): return True else: return False elif user_choice == False: if result == eval(x, y, op): return False else: return True
def generate_quiz(): x = random.randint(0, 10) y = random.randint(0, 10) op = random.choice(['+', '-', '*', '/']) error = random.randint(-1, 1) r = eval(x, op, y) + error return [x, y, op, r]
def generate_quiz(): # Hint: Return [x, y, op, result] x = randint(0,10) y = randint(0,10) op = choice(['+','-','*','/']) error = randint(-1,1) result = eval(x,y,op) + error])
def cmd_calc(self, sender, channel, cmd, args): """calc <expression>\nEvaluate an expression.""" msg = " ".join(args) try: self.privmsg(channel, str(calc.eval(msg))) except calc.CalculationException as e: self.privmsg(channel, str(e))
def generate_quiz(): x = randint(1, 10) y = randint(1, 10) op = choice(["+", "-", "*", "/"]) error = randint(-1, 1) r = eval(x, y, op) + error return [x, y, op, r, error]
def check_answer(x, y, op, result, user_choice): correct_result = eval(x, y, op) if result = correct_result: if user_choice == True: return True elif user_choice == False: return False
def genarate_quiz(): a = randint(0, 10) b = randint(1, 10) error = randint(-1, 1) op = random.choice(['+', '-', '*', '/']) t = eval(a, b, op) + error return [a, b, op, t, error]
def generate_quiz(): # Hint: Return [x, y, op, result] x = randint(0, 9) y = randint(0, 9) op = choice(["+", "-", "*", "/"]) error = randint(-1, 1) r = eval(x, y, op) + error return [x, y, op, r]
def generate_quiz(): # Hint: Return [x, y, op, result] a = randint(0, 10) b = randint(0, 10) error = choice([-2, -1, 0, 0, 0, 1, 2]) op = choice(["+", "-", "*", "/"]) s = eval(a, b, op) result = s + error return a, b, op, result
def generate_quiz(): x = randint(0, 10) y = randint(1, 10) s = randint(-5, 100) op = choice(["+", "-", "/", "*"]) #error = randint(-1,1) r = eval(x, y, op) return [x, y, op, s, r]
def check_answer(x, y, op, result, user_choice): if eval(x, y, op) == result: if user_choice == True: return True else: return False else: if user_choice == True: return False else: return True
def generate_quiz(): x = randint(0, 10) y = randint(0, 10) op = choice(['+', '-', '*', '/']) # if op == "+": # r = x+y # elif op == "-": # r = x-y # elif op == "*": # r = x*y # elif op =="/": # r = x/y # else: # print("invalid operators") error = randint(-1, 1) r = eval(x, y, op) + error return [x, y, op, r]
from random import randint, choice from calc import eval x = randint(0, 10) y = randint(1, 10) error = randint(-1, 1) op = choice(["+", "-", "*", "/"]) r = eval(x, y, op) + error output = "{0} {3} {1} = {2}".format(x, y, r, op) print(output) user_answer = input("T/F? ").upper() if user_answer == "T": if r == eval(x, y, op): print("Correct") else: print("Wrong") if user_answer == "F": if r == eval(x, y, op): print("Wrong") else: print("Correct")
from random import randint, choice from calc import eval op = choice(['+','-','*','/']) e = randint(-1,1) x = randint(0, 10) y = randint(1, 10) t = eval (x , y , op) r = t + e print ( x , op , y, '=',r) a = input ('Y/N:').upper if e == 0: if a == 'Y': print('True.') elif a == 'N': print('False.') else: if a == 'Y': print('False.') elif a == 'N': print('True.')
from random import randint, choice from calc import eval a = randint(0, 10) b = randint(0, 10) error = randint(-1, 1) op_list = ["+", "-", "*", "/"] op = choice(op_list) display_result = eval(a, b, op) + error print("{0} {3} {1} ={2}".format(a, b, display_result, op)) ans = input("Y/N ").lower() if error == 0: if ans == "y": print("Nice") elif ans == "n": print("You're wrong") elif error != 0: if ans == "y": print("You're wrong") elif ans == "n": print("Nice")
from calc import eval z = eval(3, 5, "+") print(z) # import calc # # z = calc.eval(3,5,"+")
def on_calc(self, sender, channel, msg): """Trigger handler for calculations""" try: self.privmsg(channel, str(calc.eval(msg))) except calc.CalculationException: pass
def main(): parser = optparse.OptionParser() parser.add_option('-l', '--logging-level', help='Logging level') parser.add_option('-f', '--logging-file', help='Logging file name') (options, args) = parser.parse_args() logging_level = LOGGING_LEVELS.get(options.logging_level, logging.CRITICAL) logging.basicConfig(level=logging_level, filename=options.logging_file, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') tests = { # "3+5":"8", # "9*7":"63", # "1|2":"1", # "0|2":"2", # "0&2":"0", # "2&0":"0", # "1&2":"2", # "2<-4":"0", # "2>-5":"1", # "4>=5":"0", # "4<=5":"1", # "5%3":"2", # "5%4":"1", # "5%5":"0", # "@-4":"4", # "@3":"3", # "@(@-3)*4":"12", # "3?5?9?4:3:2:1":"4", # "3?4:5":"4", # "0?4:5":"5", # "!!!!!4":"0", # "3|4|5|6":"3", # "y=1":"1", # "x=-3":"-3", # "x=y=z=4":"4", # "x=-3 | y=4":"4", # "x=4 -1 < -4":"0", # "x=4 -1 > -5":"1", # "x=4 -1 >= 5":"0", # "x=4 -1 <= 5":"1", # "(1+3)-2":"2", # "@(x=-3)":"3", # "(alpha = beta = 3 - 6/2 ? 10 : 5) & alpha*beta":"25", # "(bar = 4) + bar":"8", # "((d = d + 1) | (d = d + 456)) + d":"2", # "((e = 1 - (f = f + 1)) & f) | f":"1", } for test in tests: logging.info("\n============================================\n"+ "\ttestcalc.main\ttest is : "+str(test)+ "\n============================================") try: e = calc.Parser(test).parse() logging.info('\tAST:'+ str(e)) result = calc.eval(e) if str(tests[test]) == str(result): logging.info("\nSUCCESS!\n\tresult : "+str(result)+"\n") else: logging.warning("FAILURE:\n\tresult : "+str(result)+" != expected result : "+str(tests[test])+"\n") except: logging.critical( "\n\n+------------------------------------------\n"+ "+------------------------------------------\n"+ "\n"+ ""+str(sys.exc_info()[0])+"\n"+ "\n"+ ""+str(sys.exc_info()[1])+"\n"+ "\n"+traceback.format_exc()+"\n"+ "\n"+ "+------------------------------------------\n"+ "+------------------------------------------\n\n")
import random from calc import eval # Sinh bieu thuc x = random.randint(0, 10) y = random.randint(0, 10) error = random.choice([-2, -1, 0, 0, 0, 1, 2, 3]) op = random.choice(["+", "-", "*", "/"]) kq = eval(x, y, op) + error print(x, op, y, "=", kq) user_input = input("Your answer (Y/N)").upper() result = "" if error == 0: if user_input == "Y": result = "Yay" elif user_input == "N": result = "Nay" else: if user_input == "Y": result = "Wrong" elif user_input == "N": result = "Yay" print(result)
# r = x*y # elif op =="/": # r = x/y # else: # print("invalid operators") error = randint(-1, 1) r = eval(x, y, op) + error return [x, y, op, r] quiz = generate_quiz() # quiz = [x, y, op, r] x = quiz[0] y = quiz[1] op = quiz[2] r = quiz[3] output = "{0} {3} {1} = {2}".format(x, y, r, op) print(output) user_answer = input("Y/N ?").upper() if r == eval(x, y, op): if user_answer == "Y": print("True") else: print("False") else: if user_answer == "N": print("True") else: print("False")
import random import calc # from calc import eval x = random.randint(0, 20) y = random.randint(0, 20) error = random.randint(-2, 2) op = random.choice(["+", "-", "*", "/"]) r = calc.eval(x, y, op) + error print(x, op, y, "=", r) answer = input("Y/N:") result = None if answer == "y": if error == 0: result = "Yay" else: result = "Nay" if answer == "n": if error != 0: result = "Yay" else: result = "Nay" print(result)
def main(): parser = optparse.OptionParser() parser.add_option('-l', '--logging-level', help='Logging level') parser.add_option('-f', '--logging-file', help='Logging file name') (options, args) = parser.parse_args() logging_level = LOGGING_LEVELS.get(options.logging_level, logging.CRITICAL) logging.basicConfig(level=logging_level, filename=options.logging_file, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') tests = { # "3+5":"8", # "9*7":"63", # "1|2":"1", # "0|2":"2", # "0&2":"0", # "2&0":"0", # "1&2":"2", # "2<-4":"0", # "2>-5":"1", # "4>=5":"0", # "4<=5":"1", # "5%3":"2", # "5%4":"1", # "5%5":"0", # "@-4":"4", # "@3":"3", # "@(@-3)*4":"12", # "3?5?9?4:3:2:1":"4", # "3?4:5":"4", # "0?4:5":"5", # "!!!!!4":"0", # "3|4|5|6":"3", # "y=1":"1", # "x=-3":"-3", # "x=y=z=4":"4", # "x=-3 | y=4":"4", # "x=4 -1 < -4":"0", # "x=4 -1 > -5":"1", # "x=4 -1 >= 5":"0", # "x=4 -1 <= 5":"1", # "(1+3)-2":"2", # "@(x=-3)":"3", # "(alpha = beta = 3 - 6/2 ? 10 : 5) & alpha*beta":"25", # "(bar = 4) + bar":"8", # "((d = d + 1) | (d = d + 456)) + d":"2", # "((e = 1 - (f = f + 1)) & f) | f":"1", } for test in tests: logging.info("\n============================================\n" + "\ttestcalc.main\ttest is : " + str(test) + "\n============================================") try: e = calc.Parser(test).parse() logging.info('\tAST:' + str(e)) result = calc.eval(e) if str(tests[test]) == str(result): logging.info("\nSUCCESS!\n\tresult : " + str(result) + "\n") else: logging.warning("FAILURE:\n\tresult : " + str(result) + " != expected result : " + str(tests[test]) + "\n") except: logging.critical( "\n\n+------------------------------------------\n" + "+------------------------------------------\n" + "\n" + "" + str(sys.exc_info()[0]) + "\n" + "\n" + "" + str(sys.exc_info()[1]) + "\n" + "\n" + traceback.format_exc() + "\n" + "\n" + "+------------------------------------------\n" + "+------------------------------------------\n\n")
def test_eval(self): for postfix, val in self.exprtoval: result = calc.eval(postfix) self.assertEqual(val, result)
from random import randint, choice from calc import eval x = randint(1, 20) y = randint(1, 20) operation = choice(["+", "-", "*", "/"]) error = randint(-1, 1) display_result = eval(x, y, operation) + error # if operation == '+': # result = x + y + error # elif operation == '-': # result = x - y + error # elif operation == '*': # result = x * y + error # elif operation == '/': # result = x / y + error print(str(x) + operation + str(y) + " =", display_result) solution = input("(Y / N)") if error == 0: if solution == "y": print("Yay") if solution == "n": print("Game over!") else: if solution == "y": print("Game over!") if solution == "n": print("Yay")
from random import randint, choice from calc import eval total = 0 while True: x = randint(0, 9) y = randint(1, 9) error = randint(-1, 1) pheptinh = choice(["+", "-", "*", "/"]) r = eval(x, y, pheptinh) + error print(f"{x} {pheptinh} {y} = {r}") user_answer = input("Y/N ").upper() if user_answer == "Y": if error == 0: total += 1 else: total += 0 break elif user_answer == "N": if error == 0: total += 0 break else: total += 1 print("Your Score: ", total)
from random import randint from random import choice # from calc import eval import calc while True: x = randint(0, 10) y = randint(0, 10) op = choice(["+", "-", "*", "/"]) errors = randint(-1, 1) real_result = calc.eval(x, y, op) display_result = errors + real_result print("{0} {1} {2} = {3}".format(x, op, y, display_result)) user_input = input("Y/N: ").lower() if user_input == "y": if errors == 0: print("yura") else: print("You're wrong") break elif user_input == "n": if errors == 0: print("You're wrong") break else: print("yura")
import random from calc import eval def generate_quiz(): x = random.randint(0, 10) y = random.randint(0, 10) op = random.choice(['+', '-', '*', '/']) error = random.randint(-1, 1) r = eval(x, op, y) + error return [x, y, op, r] x, y, op, r = generate_quiz() output = "{0} {1} {2} = {3}".format(x, op, y, r) print(output) user_input = input("Y/N? ").upper() if r == eval(x, op, y): if user_input == "Y": print("Yay") elif user_input == "N": print("You're wrong") else: if user_input == "Y": print("You're wrong") elif user_input == "N": print("Yay")
from random import randint, choice from calc import eval x = randint(0, 10) y = randint(0, 10) op = choice(["+", "-", "*", "/"]) error = randint(-1, 1) display_result = eval(x, y, op) + error print("{0} {3} {1} = {2}".format(x, y, display_result, op)) answer = input("Y/N? ").lower() if (answer == "y" and error == 0) or (answer != "y" and error != 0): print("Yeah") else: print("No")
from random import randint, choice x = randint(0,9) y = randint(1,9) error = choice([-1,0,0,0,0,0,1]) notice = "" op=choice(["+", "-", "*", "/"]) #Way1: from calc import eval result=eval(x,y,op) #Way2: import calc result=calc.eval(x,y,op) hienthi=result+error # print(x, op, y, "=", hienthi) print("{0} {1} {2} = {3}".format(x,op,y,hienthi)) doan=input("Y/N?").lower() if notice == "": if error==0: if doan=="y": print("You're right") else: print("Wrong")
from calc import eval #, add (import * = import all) x = int(input("Enter x ")) y = int(input("Enter y ")) op = input("Enter operator + - * / ") r = eval(x, op, y) print(x, op, y, "=", r)