我是 Python 的初学者,我正在做一个自动售货机程序。我在这里检查了其他代码以获得我想要的答案,但它们与我的有点不同。在我的程序中,我一步一步地引导用户。我尝试了不同的方法来使总数相加,并将总数与输入的金额进行比较,以查看用户是否可以使用更多产品,但我无法让它发挥作用。非常感谢您的帮助..如果有人对改进我的代码有任何建议,请告诉我..这是我的代码:
main_menu = ["Drinks", "Chips"]
drinks_dict = {'Water':2, 'Mountain Deo':1.5, 'Juice':3}
chips_dict = {'Pringles':7, 'Popi Snack':0.5, 'Sahar':1}
total = 0
def main_menu_func():
print(main_menu)
x = str(input("Choose the category you want. Type NONE to finish."))
if( x == 'drinks'):
print(drinks_func())
elif ( x == 'chips'):
print(chips_func())
else:
print("Please pick up your items and don't forget your change.")
print(change_func())
def another_drink():
x = (input("Do you want to get another drink? Type YES or NO: "))
if (x == 'YES'):
print(drinks_func())
else:
print(main_menu_func())
def another_chips():
x = (input("Do you want to get another chips? Type YES or NO: "))
if (x == 'YES'):
print(chips_func())
else:
print(main_menu_func())
def drinks_func():
print(drinks_dict)
print("Type in the drink you want. If you do not want a drink type NONE:")
drink = str(input())
if (drink == 'Water'):
water_cost = 2
## update the total cost?
print(another_drink())
elif (drink == 'Mountain Deo'):
drink_cost = 1.5
## update the total cost?
print(another_drink())
elif (drink == 'Juice'):
drink_cost = 3
## update the total cost?
print(another_drink())
elif (drink == 'NONE'):
print(main_menu_func())
else:
print("Please enter a valid response")
print(drinks_func())
def chips_func():
print(chips_dict)
print("Please type in the chips you want")
chips = str(input())
if(chips == 'Pringles'):
chips_cost = 7
## update the total cost?
print (another_chips())
elif(chips == 'Popi Snack'):
chips_cost = 0.5
## update the total cost?
print(another_chips())
elif(chips == 'Sahar'):
chips_cost = 1
## update the total cost?
print(another_chips())
else:
print("The response you entered isn't valid. Try again.")
print(chips_func())
def change_func():
print("Thank you for buying from us.")
## update the total cost
change = coins - total
print("You paid", coins, "and you bought with", str(total), ". Your change is: ", (change))
print('Welcome to the vending machine. The maximum number of coins you can enter is 3.')
coins = (float(input('Please enter your coins: ')))
coin_type = (str(input('Are your coins AEDs? Please type YES or NO: ')))
if (coin_type == "YES"):
print("You entered", str(coins), "Dirhams. Please select the category you want.")
print(main_menu)
print("Please type in the category you want:")
category_selection = str(input())
if(category_selection == 'Drinks'):
print(drinks_func())
elif(category_selection == 'Chips'):
print(chips_func())
else:
print("Invalid response.")
#total = (drink_cost + chips_cost + candy_cost)
else:
print('We only accept AED.')