mardi 23 février 2016

How do I put mathematical operations in a list?

I need to make my code as short as possible and I need to put * - + in a list like this: op = [*,-,+]

It gives me a syntax error... I know I can put them as strings but is that the only way? My program picks a random operator from that list and makes a sum out of it, eg, sum = num1 op num2

my code:

import random

opL = [*,-,+]

def mathsQuiz(number1,operator,number2):
    sum1=number1,operator,number2
    print(sum1)

number1 = random.randint(1,12)
number2 = random.randint(1,12)
operator = random.choice(opL)

mathsQuiz(number1,operator,number2)




Aucun commentaire:

Enregistrer un commentaire