if_else语句

 -----------------以下截图来自王大鹏老师的教学

 

if (3 < 89):
    print("True")  # 假如3小于89,则打印字符串“True”
    
if (3 < 9):
    pass  # pass语句,占位符号,不执行任何语句,不起任何实际作用
#V1.0 有逻辑问题,跟业务期望不一致
score = 87
if (score >= 90):
    print("A")
if (score >= 80 ):
    print("B")
if (score >= 70 ):
    print("C")
if (score >= 60 ):
    print("D")
if (score < 60):
    print("E")

#V2.0 初级if语句,主要是理解思路,但是不是标准程序员的写作格式
score = 87
if (score >= 90):
    print("A")
if (score >= 80 and score<90):
    print("B")
if (score >= 70 and score<80):
    print("C")
if (score >= 60 and score<70):
    print("D")
if (score < 60):
    print("E")

简单的练习:判断是否及格

score = input("请输入你的成绩:")  # input返回的是string类型,不能直接跟整数比较
score = int(score)  # 通过int对象可以把字符串类型转换成整数数字类型
if (score >= 60):
    print("你已经及格!")
else:
    print("对不起,你需要重考!")

增加业务需求:能够判断好成绩,及格和不及格

score = input("请输入你的成绩:")
score = int(score)  # 通过int对象可以把字符串类型转换成整数数字类型
if (score >= 90):
    print("A")
    print("You are the best!")
    print("Thank You!")
else:
    if (score >= 60):
        print("成绩及格")
    else:
        print("成绩不及格")

score = 87
if (score > 90):
    if (score >= 95):
        print("You are the best!")
else:
    print("一般般啦")

练习;模拟上海出租车收费系统

1. 3公里以内,13元;

2. 15公里以内,每公里2.3元;

3.超过15公里,加收空驶费,为单价的50%,即:2.3+2.3*0.5=3.45元

km = input("请输入里程数:")
km = float(km)  # float表示浮点数,即小数,里程可能是小数
cost = 0
if (km <= 3):
    cost = 13
    print("费用是:{:,.1f}".format(cost))
else:
    if (km <= 15):
        cost = 13 + (km - 3) * 2.3
        print("费用是:{:,.1f}".format(cost))
    else:
        cost = 13 + 12 * 2.3 + (km - 15) * 3.45
        print("费用是:{:,.1f}".format(cost))
原文地址:https://www.cnblogs.com/jameskane/p/8495859.html