006-Python if判断

话不多说,上代码,看结果。

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 的匹配按照缩进格式匹配

结果如下图。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值