if……else……条件分支语句可以写在一行,也叫作三目运算
def qiepian(): idNo = "101110194711257" value = int(idNo[14:17][-1]) print(value) # if value % 2 == 1: # sex = '男' # else: # sex = '女' # 将上面if else 简化: sex = '男' if value % 2 == 1 else '女' print(sex)
格式化输出内容:
def qiepian(): idNo = "101110194711257" value = int(idNo[14:17][-1]) print(value) sex = '男' if value % 2 == 1 else '女' print(sex) print('证件性别位数字为:{},奇数男偶数女,所以此人为:{}'.format(value, sex))
运行结果
7 男 证件性别位数字为:7,奇数男偶数女,所以此人为:男