如何在3次循环停止后设置python?

时间:2015-03-07 23:44:53

标签: python while-loop

我是Python的新手,我想在3次之后调整我的while循环停止,请帮助!

让我们起床: - ))

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

2 个答案:

答案 0 :(得分:1)

使用计数器跟踪您循环的次数。

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

答案 1 :(得分:0)

尝试使用for循环来修复重复:

for i in range(3):
    if answer == 'No':
        print a + b + c
        answer = raw_input("Enter Yes or No: ")