-2

我是 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.')
4

1 回答 1

0

您无法计算总成本,因为您没有在函数周围传递任何成本。我建议您稍微重新安排一下,从处理主要购买的通用函数开始:

def buy_something(items_dict, credit):
    """Give the user their options, allow them to choose, return price."""

这将获取要购买的物品的字典,循环直到用户选择他们买得起的物品或"none",然后返回所选物品的价格。请注意,您有一本包含名称和价格的字典;您不需要像现在那样在其他地方硬编码价格。您还可以让用户通过项目编号进行选择,而不是输入全名(查找enumerate)。

现在您的饮料功能看起来要简单得多:

def buy_drink(credit):
    drinks_dict = {'Water': 2, 'Mountain Dew': 1.5, 'Juice': 3} 
    return buy_something(drinks_dict, credit)

您可以将机器的其他功能拆分为:

  1. 插入信贷;
  2. 调用buy_函数并从;price返回 credit
  3. credit从用户退出时进行更改。

就像是:

def main_vend_loop():
    credit = 0
    while True:
        print(...) # how much credit you have
        ui = input(...).lower() # what to do
        if ui == "insert coins":
            credit += insert_coins() # credit goes up
        elif ui == "get change":
            return get_change(credit) # finished, give change
        elif ui == "buy drink":
            credit -= buy_drink(credit) # credit goes down
        ...

注意credit现在是一个显式参数,所以你传递用户拥有的数量并在适当的地方调整它。“再次购买”功能并不是必需的,用户可以从“主菜单”中再次选择相同的功能。

于 2014-05-03T08:49:25.143 回答