我正在尝试在python中创建一台自动售货机,以进一步在课堂外学习它。我目前正在尝试打印自动售货机的饮料或零食的选项。在我添加函数select()之前,它完全打印了列表选项,一旦我添加了choose()函数,它只打印列表中的第一个项目。
import time
drinkPairs=[("Apple Juice", 1.50), ("Monster", 3.75), ("Red Bull", 3.75), ("Sprite", 2.00), ("Water", 1.00)]
drinkDict, drinkPrice = zip(*drinkPairs)
snackPairs=[('Pringles', 3.50), ('Doritos', 3.15), ('Chocolate Donuts', 2.50), ('Honey Bun', 3.75), ('Cinnamon Roll', 3.50)]
snackDict, snackPrice = zip(*snackPairs)
def select():
if 'drink' in answer.lower():
drink()
elif 'snack' in answer.lower():
snack()
else:
error()
#give selection of items and costs of items
def drink():
print('Here are the drink options:\n')
for i, p in enumerate(drinkDict):
print('{}: ${:.2f}\n'.format(p,drinkPrice[i]))
#choose()
def snack():
print('Here are the snack items')
for i, p in enumerate(snackDict):
print('{}: ${:.2f}\n'.format(p,snackPrice[i]))
choose()
def error():
print("I'm sorry, I don't understand. Please try again.\n")
time.sleep(2)
select()
def choose():
selection=input('Which item would you like?\n')
if 'apple' in selection.lower():
amountDue= amountDue + 1.50
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'monster' in selection.lower():
amountDue= amountDue + 3.75
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'bull' in selection.lower():
amountDue= amountDue + 3.75
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'sprite' in selection.lower():
amountDue= amountDue + 2.00
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'water' in selection.lower():
amountDue= amountDue + 1.00
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'pringles' in selection.lower():
amountDue= amountDue + 3.50
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'doritos' in select.lower():
amountDue= amountDue + 3.15
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'chocolate' in select.lower():
amountDue= amountDue + 2.50
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'honey' in select.lower():
amountDue= amountDue + 3.75
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
elif 'roll' in select.lower():
amountDue= amountDue + 3.50
selectTwo=input('Would you like anything else?\n')
if selectTwo.lower().startswith('n'):
amountDue()
elif any(x in selectTwo.lower() for x in options):
choose()
else:
chooseError()
else:
chooseError()
def chooseError():
print("I'm sorry ,I don't understand. Please try again {}.".format(name))
time.sleep(2)
choose()
#welcome user to vending machine
name=input('Welcome to Vending 2.0. What is your name?\n')
answer= input('Okay {}, would you like a drink or snack?\n'.format(name))
select()
答案 0 :(得分:0)
for i, p in enumerate(snackDict):
print('{}: ${:.2f}\n'.format(p,snackPrice[i]))
choose() # <===================================== problem is likely here
我假设choose()
允许用户选择其中一个项目。如果要在显示选择提示之前打印所有内容,请将choose()
移出一个缩进级别。现在它位于for循环中,因此您打印一个项目然后显示提示。如果将其移出,则会打印所有项目,然后显示提示。
for i, p in enumerate(snackDict):
print('{}: ${:.2f}\n'.format(p,snackPrice[i]))
choose() # removed one indent. now the choice will happen *after* the loop