Python 类型错误:'&燃气轮机';在';内置函数或方法';和';int';
我正在开发一个游戏,但代码有问题。这是错误,这是相当烦人的任何帮助感谢Python 类型错误:'&燃气轮机';在';内置函数或方法';和';int';,python,python-3.x,Python,Python 3.x,我正在开发一个游戏,但代码有问题。这是错误,这是相当烦人的任何帮助感谢 Traceback (most recent call last): File "main.py", line 18, in <module> elif choice > randomnumber: TypeError: '>' not supported between instances of 'builtin_function_or_method' and 'int' 回溯(最近一
Traceback (most recent call last):
File "main.py", line 18, in <module>
elif choice > randomnumber:
TypeError: '>' not supported between instances of 'builtin_function_or_method' and 'int'
回溯(最近一次呼叫最后一次):
文件“main.py”,第18行,在
elif选项>随机数:
TypeError:“内置函数”或“方法”和“int”的实例之间不支持“>”
这是密码
#!/usr/bin/env python3
import random
win = False
randomnumber = random.randint(1,100)
print("I have picked a number between 1 and 100. Try and guess it!")
choice = input("Pick a number")
if choice == randomnumber:
print("Good!!")
Win = True
elif choice > randomnumber:
print("Too high")
elif choice < randomnumber:
print("Too Low")
if Win == True:
exit
#/usr/bin/env蟒蛇3
随机输入
赢=错
randomnumber=random.randint(1100)
打印(“我选择了一个介于1和100之间的数字。试着猜一下!”)
选择=输入(“选择一个数字”)
如果选项==随机数:
打印(“好!!”)
赢=真
elif选项>随机数:
打印(“太高”)
elif选项<随机数:
打印(“太低”)
如果Win==真:
出口
您需要将用户输入转换为整数才能让游戏正常运行。一种可能的方法是在开始检查数字是否过高或过低之前,在代码中包含这一行:
choice = int(choice)
如果用户没有输入像“1.2”这样的整数或根本不是数字的东西,这将抛出一个错误
以下是一个您可以尝试的示例:
import random
win = False
randomnumber = random.randint(1, 100)
print("I have picked a number between 1 and 100. Try and guess it!")
choice = input("Pick a number\n")
choice = int(choice)
Win = False
if choice == randomnumber:
print("Good!!")
Win = True
elif choice > randomnumber:
print("Too high")
elif choice < randomnumber:
print("Too Low")
随机导入
赢=错
randomnumber=random.randint(1100)
打印(“我选择了一个介于1和100之间的数字。试着猜一下!”)
选择=输入(“选择一个数字\n”)
choice=int(choice)
赢=错
如果选项==随机数:
打印(“好!!”)
赢=真
elif选项>随机数:
打印(“太高”)
elif选项<随机数:
打印(“太低”)
要使游戏正常运行,您仍然需要添加某种形式的循环,以便用户能够随着时间的推移更接近正确的猜测。我相信您的
选择
变量是'string'类型,因为输入
函数总是将输入创建为type=string
。尝试将其转换为int并重试您的代码。您使用的是哪一版本的Python?@Employee tag显示3.x错误消息意味着您有一个名为choice
的函数,这将替换包含用户输入的变量。您的脚本中还有没有显示的代码吗?