python那些事好像不那么简单——python进阶(二)原来if-else有这么多学问

#   1. 今天是LeoLRH20周岁的生日,假设每年365天,
#   计算他过了多少个星期,余多少天

age = 20
days = 20 * 365
weeks = days // 7  # 多少个星期
day = days % 7  # 余多少天
print("小时过了:", weeks,
      "个星期,余", day, "")
# 2. 分三次输入当前的小时,分钟,秒数,
# 在终端打印已距离凌晨 0:0:0过了多少秒?

s = input("请输入小时: ")
hour = int(s)  # 将字符串转为整数
s = input("请输入分钟: ")
minute = int(s)
s = input("请输入秒: ")
second = int(s)

seconds = hour * 3600 + minute * 60 + second
print("距离0:0:0已过了", seconds, "")

这是上一篇练习的answer,小问题

进阶(二),Are you ready?

if 语句

作用:让程序根据条件选择性的执行某条语句或某些语句

说明:if 语句又叫条件语句,也叫分支语句

语法:

if 真值表达式1:
    语句块1
elif 真值表达式2:
    语句块2
elif 真值表达式3:
    语句块3
...
else:
    语句块4

语法说明:

elif子句可以有0个,1个或多个;else子句可以有0个或1个且只能放在此if语句的最后

要求:if语句内部的语句通常要以4个空格的缩进来表示包含关系,相同的缩进格式代表相同的所属级别

示例:

# 输入一个数字,判断这个数是0,还是正数,还是负数 

n = int(input("请输入一个数: "))

if n == 0:
    print("您输入的是0")
elif n > 0:
    print("您输入的是正数") 
else:
    print("您输入的是负数")

if-elif-else 示例:

# 输入一个数字,判断这个数是0,还是正数,还是负数 

n = int(input("请输入一个数: "))

if n == 0:
    print("您输入的是0")
elif n > 0:
    print("您输入的是正数") 
else:
    print("您输入的是负数")

练习:

# 1. 输入个季度 1~4 输出这个季度有哪儿几个月,
#   如果输入不是1~4的数,提示用户您的输入有误!

season = int(input("请输入一个季度(1~4): "))

if season == 1:
    print("春季有1,2,3月")
elif season == 2:
    print("夏季有4,5,6月")
elif season == 3:
    print("秋季有7,8,9月")
elif season == 4:
    print("冬季有10,11,12月")
else:
    print("您的输入有误!")
# 2. 输入一年中的月份(1~12), 输出这个月在哪儿个
#    季度,如果输入的是其它数,提示您的输入有误!

month = int(input("请输入月份: "))
if 1 <= month <= 3:
    print("春季")
elif 4 <= month <= 6:
    print("夏季")
elif 7 <= month <= 9:
    print("秋季")
elif 10 <= month <= 12:
    print("冬季")
else:
    print("您的输入有误!")

if 语句的真值表达式

if 100:print("真值")# 等同于if bool(100):print("真值")

bool(x) 返回假的情况:

x为:0, 0.0, 0+0j, False, None,  ''(空字符串)  [] 空列表  {} 空字典  set() 空集合  () 空元组  ...


if语句嵌套
if语句本身是由多条子句组成的一条复合语句
if语句可以作为语句嵌套到另一个语句的内部

示例:

# 根据输入的月份来判断是哪儿个季度
month = int(input("请输入月份(1~12): "))

if 1 <= month <= 12:
    print("是合法的月份")
    if month <= 3:
        print("春季")
    elif month <= 6:
        print("夏季")
    elif month <= 9:
        print("秋季")
    else:
        print("冬季")
else:
    print("您的输入有误!")
# 练习:
#   输入一个学生的成绩(0~100),
#   判断这个学生的成绩是优(90~100),良(80~89),及格(60~79),不及格,成绩不合法5种状态
#   (建议使用if语句嵌套)

score = int(input("请输入一个学生的成绩: "))
if 0 <= score <= 100:
    if score < 60:
        print("不及格")
    elif 60 <= score < 80:
        print("及格")
    elif score < 90:
        print("良好")
    else:
        print("优秀")
else:
    print("成绩不合法")

条件表达式

语法:表达式1 if 真值表达式 else 表达式2

作用:如果真值表达式的布尔环境值为True,则执行表达式1并返回结果的引用,否则执行表达式2并返回结果的引用

# 练习:
#   写程序,输入一个数
#     1) 用if语句计算出这个数的绝对值并打印出来
#     2) 用条件表达式计算出这个数的绝对值并打印出来

n = int(input("请输入一个数: "))

# 1) 用if语句计算出这个数的绝对值并打印出来
# 方法1
# if n < 0:
#     absolute = -n
# else:
#     absolute = n
# 方法2
absolute = n
if absolute < 0:
    absolute = -absolute
print("if语句计算的绝对值是:", absolute)

# 2) 用条件表达式计算出这个数的绝对值并打印出来
absolute = -n if n < 0 else n
print("条件表达式计算的绝对值是:", absolute)
posted @ 2019-01-31 17:01  LeoLRH  阅读(322)  评论(0编辑  收藏  举报