jeudi 12 mars 2015

How do I create an answer for an equation using randomly generated numbers and operator?

I'm wondering how to plus, subtract and multiply a randomly generated number from other random number with a random operator.


This is the code I've got so far:



ops = ['+','-','*']
op = random.choice(ops)
variable1 = random.randrange(1,10)
variable2 = random.randrange(1,10)
if op == '+':
answer = sum(var1+var2)
elif op == '-':
answer = sum(var1-var2)
elif op == '*':
answer = sum(var1*var2)


I usually get an error message: answer = sum(var1-var2) TypeError: 'int' object is not iterable


Sorry if this is a simple answer, still fresh to the whole python coding! :)





Aucun commentaire:

Enregistrer un commentaire