for

  • A+
所属分类:python
1.循环猜数字

mynum = 27

for i in range(10):
    if i<3:
        guess_num = int(input('input your guess num:'))
        if guess_num == mynum:
            print('恭喜你猜对了!')
            break
        elif guess_num > mynum:
            print('你猜的数字太大了!')
        else:
            print('你猜的数字太小了!')
    else:
        print('尝试次数过多!')
        break
2.跳出本地循环,执行下次循环
for i in range(10):
    if i==5:
        continue
    print('num:',i)
打印结果:

num: 0
num: 1
num: 2
num: 3
num: 4
num: 6
num: 7
num: 8
num: 9

3.终止循环
for i in range(10):
    if i>5:
        break
    print('num:',i)
打印结果:
num: 0
num: 1
num: 2
num: 3
num: 4
num: 5
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: