Linh Huynh
Linh Huynh

Reputation: 25

how to set a python while loop stop after 3 times?

I'm new to Python, I want to condition my while loop stop after 3 times, please, help!

Let's get up:-))

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

Answers (2)

seaotternerd
seaotternerd

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

Malik Brahimi
Malik Brahimi

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

Related Questions