Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python自动售货机零食和饮料_Python - Fatal编程技术网

Python自动售货机零食和饮料

Python自动售货机零食和饮料,python,Python,嗨,伙计们,我已经尝试创建一个工作python自动售货机一个多月了,看起来我没有取得任何进展。如果有人能帮助我,那就太好了:)这是我目前的计划: print ("Welcome to the Vending Machine\n") total = 0 dr = 1 sn = 3 money=int(input("How much money do you want to insert")) print ("Prices: Drinks: £1, Snacks: £3\n") Drinks =

嗨,伙计们,我已经尝试创建一个工作python自动售货机一个多月了,看起来我没有取得任何进展。如果有人能帮助我,那就太好了:)这是我目前的计划:

print ("Welcome to the Vending Machine\n")

total = 0
dr = 1
sn = 3
money=int(input("How much money do you want to insert"))
print ("Prices: Drinks: £1, Snacks: £3\n")
Drinks = {'Coke','Pepsi','Orange Juice', 'Apple Juice','Water'}
Snacks = {'Chocolate', 'Snickers','Twix','Peanuts','Crisp'}

state = 'subtotal'
while total <= money:
    if state != 'total':
    print('')
    print('')
    print ("\nDrinks Menue:")
    print(Drinks)
    print ("Snacks Menue:")
    print(Snacks)
    choice = input("\nEnter the item of your choice: ")
    if choice in Drinks: 
        total += dr
    elif choice in Snacks:
        total += sn
    else:
        state = 'total'
    print("\n\nThat will be a",state,"of £",total)
else:
    print("You have exceeded your inserted money good bye")
print(“欢迎来到自动售货机\n”)
总数=0
dr=1
sn=3
money=int(输入(“要插入多少钱”))
打印(“价格:饮料:1英镑,零食:3英镑”)
饮料={‘可口可乐’、‘百事可乐’、‘橙汁’、‘苹果汁’、‘水}
零食={'巧克力','Snickers','Twix','Peanuts','Crisp'}
状态='小计'

虽然total但是当我运行这个时,这个问题只有一个。所以,这里有一些解释,这样你可以学到一些东西。这似乎是一个简单的错误,但我们都会在某个时候犯这些错误,最终,缩进将成为第二天性

缩进

与某些语言不同,在Python中,缩进很重要。很多让我们看一下您的第一个
if
语句

    if state != 'total':
    print('')
    print('')
    print ("\nDrinks Menue:")
    print(Drinks)
    print ("Snacks Menue:")
    print(Snacks)
    choice = input("\nEnter the item of your choice: ")
你能看到问题吗?在一个
if
语句之后,或者更准确地说,在
之后,我们需要缩进希望由if语句执行的所有其他操作,否则Python将不知道在哪里停止!因此,通过做这个简单的改变:

if state != 'total':
    print('')
    print('')
    print ("\nDrinks Menue:")
    print(Drinks)
    print ("Snacks Menue:")
    print(Snacks)

我们没有更多的错误,您的程序现在运行。注意到缩进了吗?只需检查它们是否始终正确。

您尝试过做什么,失败的具体原因是什么?发布一些示例、完整的错误消息等。您的缩进不正确。这段代码不会运行。你可能会发现它很有用。我对此很感兴趣。我想我会在接下来的一个小时左右制定出一个面向对象的答案。为了补充这个答案,您希望缩进在整个文件中保持一致。如果在一个位置有一个选项卡,在另一个位置有空格,Python将不知道一个缩进块的开始和结束位置。python中的常见约定是使用4个空格(许多编辑器可以配置为将选项卡转换为空格)。