簡體   English   中英

我的 While 循環無法按代碼中的預期運行

[英]My While loop does not work as intended in the code

print ("Welcome To Femboii's Restaraunt")
Name = input ("What's Your Name Sir?\n").capitalize()
EvilGuys = ["Jack", "Ben", "Maria"]
if Name in EvilGuys:
    Evil_Status = input ("Are You Evil " + Name + "?\n").capitalize()
    if Evil_Status == "Yes":
        Good_Deeds = input ("How Many Good Deeds Have You Done Today " + Name + "?\n")
        Bad_Deeds = input ("How Many Bad Deeds Have You Done Today " + Name + "?\n")
        Good_Deeds = int(Good_Deeds)
        Bad_Deeds = int(Bad_Deeds)
    elif Evil_Status == "No":
        print ("Oh, So You Aren't Evil, Come In You're Welcome!")
    else:
        print ("Sorry I Didn't Understand You, So Ill Just Take It As A No")
    if Good_Deeds >= Bad_Deeds:
        print ("Oh, So You Aren't Evil, Come In You're Welcome!")
    if Good_Deeds < Bad_Deeds:
        print ("GET OUTTTT RIGHT NOWWWWWWWWWW!!!!!!!!!!")
        exit ()
print ("Welcome " + Name)
FoodMenu = ("Pizza, Crepe, Burger And Shawrma.")
DrinksMenu = ("Black Coffee, Latte, Mocha, Espresso, Crapuccino And Sparkling Water")
FoodMenuList = ["Pizza", "Crepe", "Burger", "Shawrma"]
DrinksMenuList = ["Black Coffee", "Latte", "Mocha", "Espresso", "Crapuccino", "Sparkling Water"]
Order = input ("Here's Our Food Menu\n" + FoodMenu + "\nAnd Here's Our Drinks Menu\n" + DrinksMenu + "\nChoose What Serves You The Best.\n").capitalize
while Order not in FoodMenuList or Order not in DrinksMenuList:
    if Order in FoodMenuList or Order in DrinksMenuList:
        break
    if Order not in FoodMenuList or Order not in DrinksMenuList:
        input ("Sorry, I Didn't Understand What You Said, Can You Repeat?\n").capitalize()
print (Order)

我試着做到這一點,如果他要的東西不在菜單中,我會告訴他代碼是 ai 隨便什么

無法理解他,如果他繼續這樣做,它會繼續說

Sorry, I Didn't Understand What You Said, Can You Repeat

並且 while 循環不會停止

嗯,你忘了將input(...)推入Order 目前是:

while Order not in FoodMenuList or Order not in DrinksMenuList:
    if Order in FoodMenuList or Order in DrinksMenuList:
        break
    if Order not in FoodMenuList or Order not in DrinksMenuList:
        input ("Sorry, I Didn't Understand What You Said, Can You Repeat?\n").capitalize()
print (Order)

你需要:

while Order not in FoodMenuList or Order not in DrinksMenuList:
    if Order in FoodMenuList or Order in DrinksMenuList:
        break
    if Order not in FoodMenuList or Order not in DrinksMenuList:
        Order = input ("Sorry, I Didn't Understand What You Said, Can You Repeat?\n").capitalize() # here
print (Order)

你也可以重寫你的程序,這樣就不會有重復的代碼,但這是你的選擇,呵呵。

就個人而言,我會寫這樣的東西,因為它正在檢查在while條件下是否在菜單中:

while Order not in FoodMenuList or Order not in DrinksMenuList:
    Order = input ("Sorry, I Didn't Understand What You Said, Can You Repeat?\n").capitalize()
print (Order)
while not (Order in FoodMenuList or Order in DrinksMenuList:
    if Order in FoodMenuList or Order in DrinksMenuList:
        break
    if not (Order in FoodMenuList or Order in DrinksMenuList):
        input ("Sorry, I Didn't Understand What You Said, Can You Repeat?\n").capitalize()
print (Order)

大概跟邏輯有關。 你看:

not P and not Q != not (P and Q) 

正如其他人已經指出的那樣,問題似乎出在程序的邏輯上。

while Order not in FoodMenuList or Order not in DrinksMenuList:

在這里,您要檢查訂單是否不在食物菜單中,或者訂單是否不在飲料菜單中。

這將始終為True ,因為訂單不能同時出現在食物菜單和飲料菜單中。

您要做的是將or替換為and 這樣,只有當訂單不在兩個菜單中時,它才會返回True

另一種方法是組合兩個菜單並檢查項目是否在其中:

while Order not in FoodMenuList+DrinksMenuList:

也與問題無關,但不建議使用+符號來格式化字符串:

"How Many Bad Deeds Have You Done Today " + Name + "?\n"

一個更好的方法是使用格式字符串:

f"How Many Bad Deeds Have You Done Today {Name}?\n"

在這個問題的答案中,我的回答似乎有點不受歡迎。 行中存在語法錯誤

Order = input ("Here's Our Food Menu\n" + FoodMenu + "\nAnd Here's Our Drinks Menu\n" + DrinksMenu + "\nChoose What Serves You The Best.\n").capitalize

capitalize() 是一個屬性,而不是字符串數據類型的特征,因此需要用括號調用它。

當 function 在沒有括號的情況下被調用時,一個完全不同的元素存儲在變量“Order”中,這就是代碼與 FoodMenuList 和 DrinkMenuList 中提供的關鍵字無關的原因

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM