Reputation: 25
I'm new to Python, I want to condition my while loop stop after 3 times, please, help!
a = "It's time. "
b = "Alarm rings!!! "
c = "Are you ready to get up ? "
print a + b + c
answer = raw_input("Enter Yes or No: ")
if answer =='Yes':
print "Climb Out of Bed"
while answer == 'No':
print a + b + c
answer = raw_input("Enter Yes or No: ")
Upvotes: 0
Views: 5780
Reputation: 6419
Use a counter to keep track of the number of times you've looped.
a = "It's time. "
b = "Alarm rings!!! "
c = "Are you ready to get up ? "
counter = 0
print a + b + c
answer = raw_input("Enter Yes or No: ")
if answer =='Yes':
print "Climb Out of Bed"
while answer == 'No' and counter < 3:
print a + b + c
answer = raw_input("Enter Yes or No: ")
counter += 1
Upvotes: 1
Reputation: 16711
Try using a for loop for fixed repetition instead:
for i in range(3):
if answer == 'No':
print a + b + c
answer = raw_input("Enter Yes or No: ")
Upvotes: 2