Python

4.控制流If-else

2020-03-07  本文已影响0人  小哲1998

一、实验目的

二、知识要点

流程框图
1.if-elif-else基本语法
if 判断条件1:
   执行语句1……
elif 判断条件2:
     执行语句2……
elif 判断条件3:
     执行语句3……
else:
     执行语句4……

三、实验内容

1.示例程序
a = int(input("Please input an integer:"))
b = int(input("Please input an integer:"))
c = int(input("Please input an integer:"))
if a:
    print("a is true")
elif b:
    print("b is true")
elif c:
    print("c is true")
else:
    print("All false")
a b c print
0 0 0 all false
0 0 1 c is ture
0 1 0 b is true
0 1 1 b is true
1 0 0 a is true
1 0 1 a is true
1 1 0 a is true
1 1 1 a is true

可以看出,在控制流if-else中每块语句都是独立的,按照代码顺序执行,出现true程序块结束,出现false则继续进行下一条语句,如果一直是false到程序块末尾之后执行下一个程序块。(不为零的任何值都为真)

2.真值检测

通常我们使用:

if x:
    pass

而无需使用:

if x == True:
    pass

四、实验结果

1.员工工资问题

一企业进行年终奖发放,员工底薪5000元,员工年贡献利润在10000到20000之间的抽成10%,贡献利润在20000以上的抽成20%,其余情况不抽成,你需要输入员工利润X,程序计算应发年终奖并输出。(结果精确到分)

X = float(input("Please input the profit:"))
if X > 20000:
    print("The awards is:", "{:.2f}".format(5000 + X * 0.2))
elif X < 1000:
    print("The awards is:", "{:.2f}".format(5000))
else:
    print("The awards is:", "{:.2f}".format(5000 + X * 0.1))
Please input the profit:100000
The awards is: 25000.00

Please input the profit:12000
The awards is: 6200.00

Please input the profit:200
The awards is: 5000.00
上一篇下一篇

猜你喜欢

热点阅读