0

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!

Snosluoc
  • 25
  • 4
  • `rx` is a number while `ro` is a string. Concatenating a string and a number will result in this error. You should not use `eval` in the first place, see the above linked duplicate. – deceze Sep 28 '20 at 12:23
  • 1
    This was closed rather hastily. The actual problem in your code is the fact that your `x` and `y` arrays contain integer types, and `o` contains string types. You could do `eval((str(rx) + ro + str(ry))` and it would work. – Jussi Nurminen Sep 28 '20 at 12:32

0 Answers0