[英]variable resets inside of def function python 3.4.0
我只是在一两个星期前才开始编码,所以我仍然不了解很多基础知识,但本质上发生的是,每当函数重新启动时,我的“ money”变量会重置为十,如果我将变量放在外面,则会出现错误,说“在分配之前已引用了金钱”我尝试了其他选项,例如全局语句,该问题仍然存在。
def main():
money = 10.00
print('Welcome to the vending machine.')
print('The snacks available are:')
print('1. Chips - $2.50')
print('2. Chocolate Bar - $3.00')
print('3. Water - $1.90')
print('4. Cookie - $0.85')
print('5. Skittles - $2.00')
print('6. Pringles - $4.00')
print('7. Exit')
print('You have',money,'remaining!')
a = input('What would you like to purchase?')
if a == '1':
money = money - 2.5
print('You have bought chips for $2.50 you have $',money,'remaining!')
if a == '2':
money = money - 3
print('You have bought a chocolate bar for $3.00 and have $',money,'remaining!')
if a == '3':
money = money - 1.90
print('You have bought water for $1.90 and have $',money,'remaining!')
if a == '4':
money = money - 0.85
print('You have bought a cookie for $0.85 and have $',money,'remaining!')
if a == '5':
money = money - 2.00
print('You habe bought skittles for $2.00 and have $',money,'remaining!')
if a == '6':
money = money - 4.00
print('You have bought pringles for $4.00 and have $',money, 'remaining!')
c = input('Would you like to make another purchase? Y/N').upper()
if c == 'Y':
main()
if c == 'N':
exit
else:
exit
main()
您在def main()
的开头设置money = 10
如果再次调用它,它将被重置为它,因为它是main方法范围内的局部变量。
您可以在外部定义它并将其提供为参数:
def main(money):
# no money def here, it is provided as parameter
# your code
if c == 'Y':
main(money) # pass the remainder money on
else:
exit()
main(10) # call the main() with your initial money
更好的方法是读取循环并在while
循环中处理循环, while
不是一次又一次地递归到main(...)
。
使用while的版本,商品/价格的查询字典,一些循环和检查:
def menu(items, money):
print('The snacks available are:')
for key,value in sorted(items):
if value[1]:
print(f"{key}. {value[0]} - ${value[1]}")
else:
print(f"{key}. {value[0]}")
print(f'You have ${money} remaining!')
# your wares
ex = "Exit"
what = {'1': ('Chips', 2.5), '2': ('Chocolate Bar', 3),
'3': ('Water', 1.9), '4': ('Cookie', 0.85),
'5': ('Skittles', 2), '6': ('Pringles', 4),
'7': (ex, None)}
def main(money = 10.0):
menu(what.items(), money)
while True:
a = input('What would you like to purchase? ')
# handle bad input
if a not in what:
print("Not possible. Try again:")
# reprint menu
menu(what.items(), money)
continue
if what[a][0] == ex:
print("Bye.")
break
else:
thing, cost = what[a]
if cost < money:
money -= cost
print(f'You have bought {thing} for ${cost}. You have $ {money} remaining!')
else:
print(f"Too expensive. You have $ {money} remaining!'")
c = input('Would you like to make another purchase? Y/N ').upper()
if c == 'N':
print("Bye.")
break
main()
输出:
The snacks available are:
1. Chips - $2.5
2. Chocolate Bar - $3
3. Water - $1.9
4. Cookie - $0.85
5. Skittles - $2
6. Pringles - $4
7. Exit
You have $10.0 remaining!
What would you like to purchase? 1
You have bought Chips for $2.5. You have $ 7.5 remaining!
Would you like to make another purchase? Y/N Y
What would you like to purchase? 2
You have bought Chocolate Bar for $3. You have $ 4.5 remaining!
Would you like to make another purchase? Y/N Y
What would you like to purchase? 3
You have bought Water for $1.9. You have $ 2.6 remaining!
Would you like to make another purchase? Y/N Y
What would you like to purchase? 4
You have bought Cookie for $0.85. You have $ 1.75 remaining!
Would you like to make another purchase? Y/N Y
What would you like to purchase? 5
Too expensive. You have $ 1.75 remaining!'
Would you like to make another purchase? Y/N Y
What would you like to purchase? 6
Too expensive. You have $ 1.75 remaining!'
Would you like to make another purchase? Y/N Y
What would you like to purchase? 7
Bye.
声明在函数中使用全局变量
money = 10.0
def main():
global money
money = 5.0
# ...
在函数外将变量“ money”定义为全局变量。 像这样;
money = 10.0
def main():
global money
#some stuff
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.