簡體   English   中英

我的 python 代碼在無限循環中遇到問題

[英]I am having a problem with in infinite loop in my python code

我是 Python 的初學者,並且在我的程序中遇到了無限循環問題。 它正確地生成我的打印語句,但會無限地繼續。 由於某種原因,我最后的 else 語句也不起作用。

day = "Lets see how many classes you will have today."
day += "\nInput 'Finished' when you are done. What day is today? "
day = input(day)
active = True
while True:

    if day == 'Finished':
        active = False
    elif day == 'Wednesday':
        print("You should have just 1 class today!")


    elif day == 'Thursday':
        print("You should have 4 classes today!")


    elif day == 'Friday':
        print("You should have 2 classes today! ")


    elif day == 'Saturday':
        print("You should have 4 classes today! ")


    elif day == 'Sunday':
        print("You should have 4 classes today!")


    elif day =='Monday' or 'Tuesday':
        print("You don't have any classes today. Sit back and relax!")

    else:
        print("That is not a valid day dumbass!")

而不是while True ,您應該while active循環

此外,您的最終elif條件應該是

elif day == 'Monday' or day == 'Tuesday':

您還需要根據@dspencer 的評論在循環中接受輸入

完整的解決方案是

day = "Lets see how many classes you will have today."
day += "\nInput 'Finished' when you are done. What day is today? "

active = True
while active:
    day = input(day)

    if day == 'Finished':
        active = False
    elif day == 'Wednesday':
        print("You should have just 1 class today!")


    elif day == 'Thursday':
        print("You should have 4 classes today!")


    elif day == 'Friday':
        print("You should have 2 classes today! ")


    elif day == 'Saturday':
        print("You should have 4 classes today! ")


    elif day == 'Sunday':
        print("You should have 4 classes today!")


    elif day =='Monday' or day == 'Tuesday':
        print("You don't have any classes today. Sit back and relax!")

    else:
        print("That is not a valid day dumbass!")

而不是使用 while True 使用

active = True
while active:

此外,在循環內編寫以下代碼,以便您一次又一次地接受這一天,直到遇到 day == 'Finished'。

day = input(day)

否則永遠不會跳出while循環進入無限循環

另一種解決方案是在 active 為 false 時中斷,使用 break 關鍵字中斷循環

if day == 'Finished':
        active = False
        break

這段代碼引入了太多的復雜性。 您應該在 Python 中使用字典開關進行查找。

有很多方法可以做你想做的事,但這里是你的方法的問題以及如何解決這些問題:

您的代碼幾乎可以工作,但有 4 個問題。

1:你沒有縮進你的while循環

2:你沒有給你的程序一個break循環的方法。

3:最后一個 elif 中的or語句應該寫成不同的。

4:您需要在每個 if/elif/else 循環中請求不同的一天/

這是更正后的代碼:

day = "Lets see how many classes you will have today."
day += "\nInput 'Finished' when you are done. What day is today? "
day = input(day)
active = True
while active: #This was changed

    if day == 'Finished':
         active = False #This was changed

    elif day == 'Wednesday':
        print("You should have just 1 class today!")
        day = input("enter day: ") #This was added everywhere

    elif day == 'Thursday':
        print("You should have 4 classes today!")
        day = input("enter day: ")

    elif day == 'Friday':
        print("You should have 2 classes today! ")
        day = input("enter day: ")

    elif day == 'Saturday':
        print("You should have 4 classes today! ")
        day = input("enter day: ")

    elif day == 'Sunday':
        print("You should have 4 classes today!")
        day = input("enter day: ")

    elif day =='Monday' or day == 'Tuesday': #This was changed remember that ambiguity is your enemy!
        print("You don't have any classes today. Sit back and relax!")
        day = input("enter day: ")
    else:
        print("That is not a valid day!")
        day = input("enter day: ")

暫無
暫無

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

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