话不多说,上代码,看结果。
print("1、##################################")
a = input('输入一个数字:')
a = eval(a) # a = int(a) 也行
# 单分支判断
if 1+a < 10:
print('条件满足时执行的代码')
print("2、##################################")
b = input('输入一个数字:')
b = eval(b) # b = int(b) 也行
# 双分支判断
if 1 + b < 5:
print('条件满足时执行的代码')
else:
print('条件不满足时执行的代码')
print("3、##################################")
# if嵌套
people = input('你是北方人(0)还是南方人(1)? :')
taste = input('豆腐脑吃咸的(0)还是甜的(1)? :')
people = eval(people)
taste = eval(taste)
if people == 0:
print('你是北方人')
if taste == 0:
print('你豆腐脑吃咸的。')
else:
print('你豆腐脑吃甜的。')
else:
print('你是南方人')
if taste == 0:
print('你豆腐脑吃咸的。')
else:
print('你豆腐脑吃甜的。')
print("4、##################################")
# if 多分支判断
people = input('你是北方人(0)还是南方人(1)? :')
taste = input('豆腐脑吃咸的(0)还是甜的(1)? :')
people = eval(people)
taste = eval(taste)
if people == 0 and taste == 0:
print('你是北方人')
print('你豆腐脑吃咸的。')
elif people == 0 and taste == 1:
print('你是北方人')
print('你豆腐脑吃甜的。')
else:
print('你是南方人')
if taste == 0:
print('你豆腐脑吃咸的。')
elif taste == 1:
print('你豆腐脑吃甜的。')
# 嵌套不要写的太深!! 注意if else 的匹配按照缩进格式匹配
结果如下图。