-1
import random
from browser import timer
operators = ['*', '/', '+', '-']
number = input('How many problems would you like?')
number = int(number)
counter = 1

while counter <= number:
    first = random.randint(0,10)
    second = random.randint(0,10)
    randoperator = random.choice(operators)
    problem = '{} {} {} {}'.format(first, randoperator, second, "=  ")
    answer = input(problem)
    correct = problem
    counter+=1

I have tried putting this in but it doesn't run anything

if problem == answer:
            print("Correct!")
ThisIsAQuestion
  • 1,887
  • 14
  • 20
nate
  • 3
  • 1
  • `problem` is a string like `"5 + 4 = "`. `answer` is a string, for example `"9"`. Why do you expect these two strings to be equal? You need to calculate the expected answer using `first`, `second`, and `randoperator` and compare _that_ with the entered `answer` (converted to an `int` or `float`) – Pranav Hosangadi Dec 08 '20 at 20:43

1 Answers1

2

You need to actually do the calculation to find out what the answer is. Here's a quick and dirty way to do that:

import random
operators = ['*', '/', '+', '-']
number = input('How many problems would you like?')
number = int(number)
counter = 1

while counter <= number:
    first = random.randint(0,10)
    second = random.randint(0,10)
    randoperator = random.choice(operators)
    problem = '{} {} {}'.format(first, randoperator, second)
    answer = input(problem + ' =  ')
    if eval(problem) == float(answer):
        print("Correct!")
    counter+=1

Using eval is not a great idea for reasons outlined in the answers to this question. In your case, you already know the two integers and the operator, so finding the expected answer without eval is pretty easy. Say you define a function that can do this for you:

def arithmetic(op, a, b):
    if op == "+":
        return a + b
    elif op == "-":
        return a - b
    elif op == "*":
        return a * b
    elif op == "/":
        return a / b

Then call this function to get the expected answer and compare that with the answer that the user gave,

while counter <= number:
    first = random.randint(0,10)
    second = random.randint(0,10)
    randoperator = random.choice(operators)
    problem = '{} {} {}'.format(first, randoperator, second)
    answer = input(problem + ' =  ')
    if arithmetic(randoperator, first, second) == float(answer):
        print("Correct!")
    counter+=1
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
ThisIsAQuestion
  • 1,887
  • 14
  • 20
  • Even though the use of `eval` in this case seems safe, it's not recommended when other methods of evaluating the expected answer exist. -1 for propagating bad programming practices without including a caveat – Pranav Hosangadi Dec 08 '20 at 20:45
  • @PranavHosangadi I did say "quick and dirty". This is clearly not critical code and this will work for this purpose. – ThisIsAQuestion Dec 08 '20 at 20:48
  • what Ted said. I edited your answer to include an alternative solution because it's not at all complicated enough to justify the use of `eval`. Even if this is a benign case where there's _no way_ using `eval` could be unsafe, it's not a great thing to suggest, especially to a beginner, without some context into why. – Pranav Hosangadi Dec 08 '20 at 20:59