Reputation: 27
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!")
Upvotes: 1
Views: 71
Reputation: 399
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!")
Upvotes: 4
Reputation: 604
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: ")
Upvotes: 2
Reputation: 901
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
Upvotes: 2