1.0 if 判断
if 4 > 5:
print('我请你喝酒')
else:
print('我请你吃饭')
多级判断
num = input('请输入你猜的数字: ')
#input 输入的是字符串,所以数字要要加''
if num == '1':
print('一起抽烟')
elif num == '2':
print('一起喝酒')
elif num == '3':
print('一起吃饭')
else:
print('各回各家')
案例3
score = int(input("请输入分数:"))
if score > 100:
print("我擦,最高才100.。。")
elif score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print('C')
elif score >= 40:
print('D')
else:
print('太笨了')
2.0 while 循环的应用
案例一打印1~100
count = 1
flag = True #满足条件,执行循环
while flag:
print(count)
count = count + 1
if count > 100:
flag = False #False 退出循环
第二种方法
count = 1
while count <= 100: #在循环的时候就指定条件
print(count)
count = count + 1
案例二打印1+2+3~+100
count = 1
sum = 0
while count <= 100:
sum = sum + count
count = count + 1
print(sum)
3.0跳出循环---------------break
count = 1
while True:
print(count)
count = count + 1
if count > 200:
break #当count > 200时跳出循环
利用 break 打印1~100
count = 1
while True:
print(count)
count = count + 1
if count > 100:
break
4.0 结束本次循环,开始下次循环---------------continue
print(111)
count = 1
while count < 20:
print(count)
continue
count = count + 1
案例一:打印1,2,3,4,5,6,8,9,10
count = 0
while count <= 9:
count = count + 1
if count == 7:
continue
print(count)
网友评论