简体   繁体   English

我的 python 代码在无限循环中遇到问题

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

I am a beginner in Python and having a problem with an infinite loop in my program.我是 Python 的初学者,并且在我的程序中遇到了无限循环问题。 It produces my print statements correctly but continues infinitely.它正确地生成我的打印语句,但会无限地继续。 My else statement at the end also does not work for some reason.由于某种原因,我最后的 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!")

Rather than while True , you should loop while active而不是while True ,您应该while active循环

Additionally, your final elif condition should be此外,您的最终elif条件应该是

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

You also need to accept input in the loop as per @dspencer's comment您还需要根据@dspencer 的评论在循环中接受输入

Complete solution is完整的解决方案是

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!")

Instead of using while True use而不是使用 while True 使用

active = True
while active:

Also, code the following inside the loop so that you accept the day again and again until day == 'Finished' is encountered.此外,在循环内编写以下代码,以便您一次又一次地接受这一天,直到遇到 day == 'Finished'。

day = input(day)

otherwise, it will never come out of the while loop and enter into infinite loop否则永远不会跳出while循环进入无限循环

Another solution is to break when active is false break the loop using break keyword另一种解决方案是在 active 为 false 时中断,使用 break 关键字中断循环

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

There is a bit too much complexity introduced in this code.这段代码引入了太多的复杂性。 You should look up using a dictionary switch in Python.您应该在 Python 中使用字典开关进行查找。

There are many ways to do what you'd like, but here are the issues with your approach and how to fix them:有很多方法可以做你想做的事,但这里是你的方法的问题以及如何解决这些问题:

Your code almost works, but it has 4 problems.您的代码几乎可以工作,但有 4 个问题。

1: You did not indent your while loop 1:你没有缩进你的while循环

2: You didn't give your program a way to break the loop. 2:你没有给你的程序一个break循环的方法。

3: The or statement in the last elif should be written differently. 3:最后一个 elif 中的or语句应该写成不同的。

4: You need to request a different day in each if/elif/else loop/ 4:您需要在每个 if/elif/else 循环中请求不同的一天/

Here is the corrected code:这是更正后的代码:

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