lundi 28 septembre 2020

How do I use a list to store operators that I can use in a function? [duplicate]

I am very new to python and I would really appreciate the help.

If I write the eval statement where I input the values in manually it works no problem

x = input("Enter a number: ")
y = input("Enter another number: ")
o = input("Enter calculation symbols for calculation you want to perform: ")

result = eval(x + o + y)
print("result is: %s" %result)

But when I try to write the eval statement pulling variables from a list I get an “unsupported operand type(s) for +: 'int' and 'str'” error import random

x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [1, 2, 3, 4, 5, 6, 7, 8, 9];
o = ['+', '-', '*', '/'];

rx = random.choice(x)
ry = random.choice(y)
ro = random.choice(o)

result = eval(rx + ro + ry)
print("The result is: %s" %result)

I have tried changing the variable type but it seems at least to me that the issue is with the operator variable type and I have no idea how to correct that.

Thanks for the help!




Aucun commentaire:

Enregistrer un commentaire