餐厅菜单

时间:2016-12-03 00:07:49

标签: python loops

python noobie在这里。对于学校的练习题,我想做一个餐馆菜单。在检查用户名是否有效之后,用户应该输入他们的名字并完成制作三明治的程序。这个问题要求我们制作一个菜单功能,该功能假定采用choiceList的参数,最小选择和最大选择。

示例:询问用户他们在三明治上需要多少浇头。最小值至少为1个顶部和最多3个浇头。

我不明白的是,我想如何制作一个通用循环来遍历这些单独的列表,而不需要在给定的值中进行硬编码。我希望这是有道理的,我试着尽力说出来,但这里有一个例子,它看起来像我的代码和我的代码。

示例Output Example of what it's suppose to look like

# Input:
# user's name
# wrapper choice (min 1, max 1)
# protein choice (min 1, max 1)
# toppings choices (min 1, max 3)
# sauce choice (min 0, max 1)

# Processing:

# Output:
# User's name followed by their protein choice, wrapper choice, toppings choices, and sauce.

def menuModule( choiceList, minimumChoices, maximumChoices ):
    order = []
    index = 0

    for i in choiceList:
        index = index + 1 
        print( i )
        choice = input( "What is your choice?" )

        if choice == index:
            order.append( choiceList[index] )

dirtyNames = [ "mud", "dirt", "dust", "booger", "diaper" ]
valid = True
wrapChoices = [ "[1]sesame seed bun", "[2]soft tortilla shell" ]
proteinChoices = [ "[1]chicken", "[2]beef", "[3]tofu" ]
toppingChoices = [ "[1]tomato", "[2]lettuce", "[3]pickles", "[4]cheese", "[5]onions" ]
sauceChoices = [ "[1]ketchup", "[2]mayonaise", "[3]McCalorie Secret Sauce" ]


while valid:
    name = str( input( "What is your name? " ) )
    if name in dirtyNames:
        print( "I'm sorry, that name is not allowed at McThoseguys." )
        continue
    elif name.isdecimal() is True:
        print( "I'm sorry, that is not a name." )
        continue
    elif any( substring in name.lower() for substring in dirtyNames ):
        print( "I'm sorry, that name is not allowed at McThoseguys." )
    else:
        print( "Hello " + name + ", welcome to McThoseguys!" )
        valid = False
        menuModule( wrapChoices, 1, 1 )

2 个答案:

答案 0 :(得分:0)

这是一小段代码运行:

toppingChoices = [ (1,"tomato"), (2,"lettuce"), (3,"pickles"), (4,"cheese"), (5,"onions") ]

def menuModule( choiceList, minimumChoices, maximumChoices ):
    order = []
    num_choices = 0
    choices_dict = dict(choiceList)
    for choice_index in choices_dict:
        print("[{}] {}".format(choice_index, choices_dict[choice_index]))

    while num_choices < maximumChoices:
        choice = input("What is your choice? Type q to exit: ")
        if choice is "q" and num_choices >= minimumChoices:
            break
        elif choice is "q" and num_choices < minimumChoices:
            print("You must select at least {} items".format(minimumChoices))
            continue

        if int(choice) in choices_dict.keys():
            num_choices += 1
            order += [choices_dict[int(choice)]]
        else:
            print("Choice is not in the list")

    print(order)


menuModule(toppingChoices, 1, 3)

答案 1 :(得分:0)

我同意虽然有效是这种输入的好概念。

避免在列表项中包含[1]tomatoes, [2]onions个数字标记,这不是一个好习惯。最好在打印实际项目名称时添加它们,例如

for item in choice_list: print ("["+str(index)+"] "+ item) index += 1

[1] sesame seed bun
[2] soft tortilla shell

如果您决定使用()而不是[],则有一天会更容易维护;)

此外,您也可以尝试递归,也可以先将所有选择函数及其参数添加到单独的列表中,例如:

choices = [(wrapChoices, 1, 1), (proteinChoices, 1, 1), (toppingChoices, 1, 3), (sauceChoices, 0, 1)]

然后在定义中,你可以添加'idx'参数,这样你就可以从它自己调用函数,如:

def menuModule(choice_list, minimumChoices, maximumChoices, idx):

...
# and call it at the end again with
 idx += 1
 menuModule(*choices[idx], idx)

通过这种方式,您可以循环选择所有选项,但是当idx达到选择的长度时,需要退出。

作为一种做法,也许最好先为每个选择制作单独的功能,然后再尝试使它们成为“通用”

希望它有所帮助!

相关问题