python noobie here。對於在學校的練習題,我想要製作一個餐館菜單。在檢查用戶名是否有效之後,用戶應該輸入他們的名字並且執行製作三明治的程序。這個問題要求我們創建一個菜單函數,這個菜單函數假設要使用choiceList的參數,最小選擇和最大選擇。餐廳菜單的通用循環
示例:詢問使用者他們在三明治上需要的配料數量。最小值至少爲1個頂部和頂部3個澆頭。
我不明白的是我如何做一個通用循環來循環這些單獨的列表中沒有給定值的硬編碼。我希望這是有道理的,我試圖把它說得最好,但這裏是一個例子,它看起來像什麼和我的代碼。
# 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)