转到下一个函数前列表未完全打印

2024-10-02 04:35:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python创建一个自动售货机,以便在课堂之外进一步学习它。我目前正在尝试打印自动售货机的饮料或零食选项。在我添加choose()函数之前,它会完全打印列表选项,在我添加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()

Tags: inyouforinputiflowerelselike
1条回答
网友
1楼 · 发布于 2024-10-02 04:35:48
    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

相关问题 更多 >

    热门问题