import random
import operator
op = {"+":operator.add,
"-":operator.sub,
"*":operator.mul}
num1 = random.randint(0,10)
num2 = random.randint(0,10)
ops = random.choice(list(op.keys()))
print (num1 + int(ops) + num2)
我正在尝试进行随机数学问题测验,但我无法解决为什么我一直收到错误:
Traceback (most recent call last):
File "N:/Computer science/A453/Test.py", line 12, in <module>
print (num1 + int(ops) + num2)
ValueError: invalid literal for int() with base 10: '-'
>>> ================================
答案 0 :(得分:1)
尝试强制使用num1和num2上的字符串。因为你正在使用+,你应该用字符串操作。
print (str(num1) + ops + str(num2))