while循环

count = 0
while True: #True表示真的意思,即条件成立
    print(count)
    count = count + 1
    if count == 1000:
        break    #break是结束循环
print(count)

2.while-else 用法

 实现功能:猜年龄,如果猜对就不猜了,猜错只能猜三次

 

old_boy_age = "56"
count = 0
while count<3:
# for i in range(3):
    guess_age = input('guess_age:')
    if guess_age == old_boy_age:
        print('yes,you gei it')
        break
    elif guess_age < old_boy_age:
        print('think bigger')
    else:
        print('think smaller')
    count += 1
else:
     print("you have tried many times...")

如果用for循环:

old_boy_age = "56"
#count = 0
#while count<3:
for i in range(3):
    guess_age = input('guess_age:')
    if guess_age == old_boy_age:
        print('yes,you gei it')
        break
    elif guess_age < old_boy_age:
        print('think bigger')
    else:
        print('think smaller')
  #  count += 1
else:
     print("you have tried many times...")

 

 

3. 实现功能:猜年龄,猜对结束,猜错三次会问要不要继续猜

old_boy_age = "56"
count = 0
while count < 3:
    guess_age = input('guess_age:')
    if guess_age == old_boy_age:
        print('yes,you gei it')
        break
    elif guess_age < old_boy_age:
        print('think bigger')
    else:
        print('think smaller')
    count = count + 1
    if count == 3:
        continue_confirm = input('do you keep guessing?')
        if continue_confirm != 'n':
            count = 0

 

posted on 2017-05-08 22:41  sunnnyy  阅读(131)  评论(0)    收藏  举报