Python中的控制流(break & continue & pass )

1. break

2. continue

3. pass

例:

#break & continue example
number = 59
while True:
    guess = int(input('Enter an integer : '))
    if guess == number:
        # New block starts here
        break       #直接跳出当前循环,不再进行循环,直接执行循环后面的内容
        # New block ends here
    if guess < number:
        # Another block
        print('No, the number is higher than that, keep guessing')
        continue        #跳过当前循环剩下的部分,而重新回到while循环的开始
        # You can do whatever you want in a block ...
    else:
        print('No, the number is a  lower than that, keep guessing')
        continue
        # you must have guessed > number to reach here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
print('Done')


# continue and pass difference

a_list = [0, 1, 2]
print("using continue:")
for i in a_list:
    if not i:
        continue        #忽略continue后面的内容,直接返回到下一轮循环的开始
    print(i)

print("using pass:")
for i in a_list:
    if not i:
        pass        #仅仅忽略当前,直接运行后面的代码
    print(i)

猜你喜欢

转载自blog.csdn.net/qlulibin/article/details/79734729