gpt4 book ai didi

python - 变量不会更新

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:40 25 4
gpt4 key购买 nike

使用基本计算器程序制作了我的第一个学校程序,但添加了一个变量,不确定它应该是全局变量还是局部变量。它计算得很好,但不会将“tribble”作为新整数返回。

tribble = 1

def buy():
x=int(raw_input("How many?"))
return (tribble + x);

def sell():
x=int(raw_input("How many?"))
return (tribble - x);

def breed():
x=int(raw_input("Multiply them by 2"))
return (tribble * x);

def cull():
x=int(raw_input("Kill half your Tribbles"))
return (tribble / x);

print "1: Buy"
print "2: Sell"
print "3: Breed"
print "4: Cull"
print "0: QUIT"

while True:

CHOICE = int(raw_input("What will you do with your Tribbles?"))

if CHOICE == 1:
print "Buying Tribbles"
print buy()
print "You have" + str(tribble) + "Tribbles."

elif CHOICE == 2:
print sell()
print 'Selling Tribbles'
print "You have" + str(tribble) + "Tribbles."

elif CHOICE == 3:
print 'Breeding Tribbles, good luck.'
print breed()
print "You have" + str(tribble) + "Tribbles."

elif CHOICE == 4:
print "You're killing them!!"
print cull()
print "You have" + str(tribble) + "Tribbles."

elif CHOICE == 0:
exit()

最佳答案

您是否正在尝试更新 tribble 的值?

然后您应该使用以下命令更新您的变量,而不是打印 buy():tribble = buy()

这样您就知道该值已更新为函数 buy() 的结果。

对其他功能也做同样的事情。

希望对您有所帮助。

关于python - 变量不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41155703/

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