1

I am a beginner in Python and having a problem with an infinite loop in my program. It produces my print statements correctly but continues infinitely. My else statement at the end also does not work for some reason.

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!")
4
  • 1
    ...or while active: and want to bring day = input(day) inside loop. Commented Apr 1, 2020 at 2:46
  • In your own words, what are you expecting to cause the loop to exit? Commented Apr 1, 2020 at 3:11
  • The last elif in your code should be elif day == 'Monday' or day == 'Tuesday' Commented Apr 1, 2020 at 3:11
  • @KevinFloyd If you found an answer that fixed your problem, mark it as accepted Commented Apr 1, 2020 at 14:38

3 Answers 3

4

Rather than while True, you should loop while active

Additionally, your final elif condition should be

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

You also need to accept input in the loop as per @dspencer's comment

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!")
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of using while True use

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 = input(day)

otherwise, it will never come out of the while loop and enter into infinite loop

Another solution is to break when active is false break the loop using break keyword

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

Comments

2

There is a bit too much complexity introduced in this code. You should look up using a dictionary switch in 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.

1: You did not indent your while loop

2: You didn't give your program a way to break the loop.

3: The or statement in the last elif should be written differently.

4: You need to request a different day in each if/elif/else loop/

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.