python-循环|今日所学-2018-12-27

2种循环方法

 

For后面的else,是正常执行完i之后执行,若中间break,则不执行
for i in range(3):
    age = input("you guss the oldboy's age:")
    if age == oldboy_age:
        print("you are right")
        break
    elif age > oldboy_age:
        print('big')
    else:
        print('small')
else:
    print('you have tried too many times’)
 
while判断,若count不满足条件,则执行else
count = 0
while count<3:
    age = input("you guss the oldboy's age:")
    if age == oldboy_age:
        print("you are right")
        break
    elif age > oldboy_age:
        print('big')
    else:
        print('small')
    count +=1
else:
    print('you have tried too many times’)
 
可手动选择是否循环:
count = 0
while count<3:
    age = input("you guss the oldboy's age:")
    if age == oldboy_age:
        print("you are right")
        break
    elif age > oldboy_age:
        print('big')
    else:
        print('small')
    count +=1
    if count ==3:
        keep_guess = raw_input('do you want contius to guess:')
        if keep_guess != 'n':
            count = 0
 
Continue是结束本次循环,进入下次循环

posted on 2017-12-28 12:02  梁艳's_blog  阅读(145)  评论(0编辑  收藏  举报

导航