Python中的自动售货机 - 加起来成本

时间:2014-05-03 08:22:40

标签: python python-3.4

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

1 个答案:

答案 0 :(得分: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进行更改。
  4. 类似的东西:

    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现在是一个显式参数,因此您可以传递用户拥有的金额并在适当的位置进行调整。 “再次购买”功能不是必需的,用户可以从“主菜单”再次选择相同的功能。