在比较随机数和用户输入时接收不支持的操作数错误。

时间:2017-10-20 09:18:47

标签: python python-3.x random

当我尝试从用户输入的数字中减去使用randint函数生成的随机数时,我收到以下错误。

希望有人打电话帮助我。

追踪(最近一次通话):   文件" C:\ Users \ vishal.mohan.gada \ Documents \ GIt \ PythonLearning \ Class \ exercise9.py",第5行,in     如果(rannum输入== 0): TypeError:不支持的操作数类型 - :' int'和'功能'

我的代码:

import random
rannum=random.randint(1,9)
print(rannum)
inputUser=int(input("Please guess one number between 1 to 10: "))
if(rannum-input==0):
    print("Your Guess was right! Congratulations")
elif(rannum-input<0):
    print("Your guess was high")
else:
    print("Your guess was low")

2 个答案:

答案 0 :(得分:0)

您的变量名称不正确。它应该是rannum-inputUser而不是rannum-input

答案 1 :(得分:0)

if(rannum-input==0): elif(rannum-input<0):正在使用input,即内置函数的标识符,而不是inputUser,包含插入数据的变量。取代它们似乎解决了这个问题:

import random
rannum=random.randint(1,9)
print(rannum)
inputUser=int(input("Please guess one number between 1 to 10: "))
if(rannum-inputUser==0):
    print("Your Guess was right! Congratulations")
elif(rannum-inputUser<0):
    print("Your guess was high")
else:
    print("Your guess was low")