记录一下进行多层判断时的不同写法。
e = 20
f = 8
if e < f:
print("e is less than f")
elif e == f:
print("e is equal to f")
elif e > f + 10:
print("e is greater than f by 10")
else:
print("e is greater than f")
g = 8
h = 8
if g < h:
print("g is less than h")
else:
if g == h:
print("g is equal to h")
else:
print("g is greater than f")
需要体会python中通过缩进体现逻辑和结构的形式
学习一下写函数:
name1 = "xiaoshuang"
height_m1 = 2
weight_kg1 = 90
def BMI_calculator(name, hight_m, weight_kg):
BMI = height_m1 / (weight_kg**2)
print("BMI:")
print(BMI)
if BMI > 25:
return (name) + " is over weight"
else:
return (name) + " is not over weight"
result1 = BMI_calculator(name1, height_m1, weight_kg1)
print(result1)
BMI:
0.0002469135802469136
xiaoshuang is not over weight
注意:只有return是返回给result的结果,所以需要print显示,其他的是运行function的结果。
另外,今天学到的是需要耐心的对待报错,静下心去看报错的类型和原因,而不是看到报错就慌乱。
python is fun, let's keep trying~
网友评论