第2章 控制流

一、elif

name = "tj"
age = 10
if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')
else:
    print('You are neither Alice nor a little kid.')

 

二、while

while True:
    print('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password? (It is a fish.)')
    password = input()
    if password == 'swordfish':
        break
print('Access granted.')

 

三、for

import random as r
import sys
for i in range(10):
print(r.randint(1, 10))
sys.exit() # 结束程序

 

posted @ 2020-09-29 19:17  老谭爱blog  阅读(84)  评论(0)    收藏  举报