gpt4 book ai didi

python - 类型错误: '>' 和 'function' 实例之间不支持 'int'

转载 作者:行者123 更新时间:2023-12-01 22:10:48 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的投币式游戏,当我运行它时出现错误:

if ticket > 50:
TypeError: '>' not supported between instances of 'function' and 'int'

而且我真的不明白我做错了什么。对我来说似乎一切都很好但是当我运行它时它给了我我以前写的那个错误。非常感谢任何帮助。这是我的代码:

def flip():
if player_ticket > 50:
if ticket > 50:
result = Label(mainFrame, text="YOU WON!!!", fg="green")
result.grid(row=3, columnspan=2)
else:
result = Label(mainFrame, text="YOU LOST!!!", fg="red")
result.grid(row=3, columnspan=2)

else:
if player_ticket < 50:
if ticket < 50:
result = Label(mainFrame, text="YOU WON!!!", fg="green")
result.grid(row=3, columnspan=2)
else:
result = Label(mainFrame, text="YOU LOST!!!", fg="red")
result.grid(row=3, columnspan=2)

def ticket_heads():
global player_ticket
player_ticket = decimal.Decimal(random.randrange(0, 50))

def ticket_tails():
global player_ticket
player_ticket = decimal.Decimal(random.randrange(50, 100))

def ticket():
global ticket
ticket = decimal.Decimal(random.randrange(0, 100))

heads_text = Label(mainFrame, text="Heads")
heads_text.grid(row=0, column=0)

tails_text = Label(mainFrame, text="Tails")
tails_text.grid(row=0, column=1)

heads_select = Button(mainFrame, text="Select", command=ticket_heads)
heads_select.grid(row=1, column=0)

tails_select = Button(mainFrame, text="Select", command=ticket_tails)
tails_select.grid(row=1, column=1)

flipit = Button(mainFrame, text="Flip It!", command=flip)
flipit.grid(row=2, columnspan=2)

最佳答案

您的 ticket 变量被您的函数变量 ticket 覆盖。这会让你得到一个错误,这意味着你不能将橙子(函数)与苹果(整数)进行比较。重命名其中一个,它应该可以正常工作。

要么替换:

global ticket
ticket = decimal.Decimal(random.randrange(0, 100))

与:

global anything_but_ticket
anything_but_ticket = decimal.Decimal(random.randrange(0, 100))

:

def ticket():

与:

def anything_but_ticket():

关于python - 类型错误: '>' 和 'function' 实例之间不支持 'int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48046884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com