if 条件: 代码1 代码2 代码3 代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行) cls ='humale' gender = 'female' age = 18 if cls =='human' and gender =='female' and age >16 and age < 22: print('开始表白') print('end...')
elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么。
cls = 'human' gender = 'female' age = 28 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('开始表白') elif cls == 'human' and gender == 'female' and age > 22 and age < 30: print('考虑下') else: print('阿姨好')
while 条件为 true
代码块
while 1: egon_age = 73
age = input('请输入你猜的年龄》》》')
age = int(age)
if age == egon_age:
print('猜对了')
elif age > egon_age:
print('猜大了')
elif age < egon_age:
print('猜小了')
print('我跳出while循环了')
while 1: egon_age = 73
age = input('请输入你猜的年龄》》》') # 73
if age == egon_age: # 成立
print('猜对了')
break
elif age > egon_age:
print('猜大了')
elif age < egon_age:
print('猜小了')
prize = {0: 'zhangyan', 1: 'bu_wawa', 2: 'nick', 3: 'balloon_wawa'}
while 1: egon_age = 73
age = input('请输入你猜的年龄》》》') # 73
age = int(age) # 73
if age == egon_age: # 成立
while True:
print(F'猜对了!!!\n请选择下列奖项中的一个:{prize}')
choice = input('请选择你需要的奖品。')
choice = int(choice)
if choice == 2:
print('大傻,这个不能给你')
print('重新选择\n')
else:
print(f'大傻,{prize[choice]}这个你也要\n')
break
break
elif age > egon_age:
print('猜大了')
elif age < egon_age:
print('猜小了')
count = 1 while count < 101: # 1<101,2<101,count=50
if count == 50:
count += 1
continue # 不执行下面的代码
print(count) # 1,2
count += 1 # count=2,count=3
break直接终止整个while循环;continue只是不执行下面的代码,但是会继续循环下去
for循环的循环次数受限于容器类型的长度,而while循环的循环次数需要自己控制。for循环也可以按照索引取值。
for i in 列表和字典
i就是列表的每个元素
for循环调出本层