2016-12-03 107 views
1

python noobie here。對於在學校的練習題,我想要製作一個餐館菜單。在檢查用戶名是否有效之後,用戶應該輸入他們的名字並且執行製作三明治的程序。這個問題要求我們創建一個菜單函數,這個菜單函數假設要使用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) 

回答

0

而有效循環是正確的概念。這處理一個的菜單。現在,您需要循環遍歷各個列表部分以及更大的列表來保存菜單。

menu_sections = [ 
       wrapChoices, 
       proteinChoices, 
       toppingChoices, 
       sauceChoices 
       ] 

for section in menu_sections: 
    valid_choice = False 
    while !valid_choice: 
     # Get user's choice 
     # Check validity ... if choice <= len(section) 

這會讓你感動嗎?

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) 
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達到選擇長度退出。

這樣的做法,也許是更好的,你先單獨的函數對每個選擇,然後稍後再試,讓他們「通用」

希望這有點幫助!