我使用的是python3.4。我收到错误:
Traceback (most recent call last):
File "H:/GCSE's/Computing/Assesment/1/School Grading Script.py", line 44, in <module>
if answer== eval(num1<currentop>num2):
TypeError: unorderable types: int() < str()
尝试执行此代码时
^{pr2}$我要做的是对照随机生成的变量来检查答案
使用
eval
是一种很糟糕的做法,应该避免。对于您正在尝试的操作,您应该使用operator。在更改数据结构以使用字典,以便更轻松地执行操作。像这样:
res输出:
^{pr2}$要在中应用随机实现,请使用字典
keys()
对其执行random.choice
:应用随机的简单示例:
你在混合}和{},
int
,num1
和{currentop
。把它们投射到str
上,就可以了:PS:您应该使用
eval()
来avoid。在您需要将其转换为字符串,还需要引用“不正确的”:
正如其他人指出的,除非出于测试目的,否则不要使用eval。在
相关问题 更多 >
编程相关推荐