如需转载本网站内容,请标明转载来源,且保持作品完整性
#CalBMIv3.py
height,weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]:"))
bmi = weight / pow(height,2)
print("BMI 数值为:{:.2f}".format(bmi))
who,nat = "",""
if bmi < 18.5:
who,nat = "偏瘦","偏瘦"
elif 18.5 <= bmi < 24:
who,nat = "正常","正常"
elif 24 <= bmi <25:
who,nat = "正常","偏胖"
elif 25 <= bmi < 28:
who,nat = "偏胖","偏胖"
elif 28 <= bmi < 30:
who,nat = "偏胖","肥胖"
else:
who,nat = "肥胖","肥胖"
print("BMI指标为:国际'{0}',国内'{1}'".format(who,nat))
遍历循环!!!
for <循环变量> in <遍历结构>:
<语句块>
-
由保留字for和in组成,完整遍历所有元素后结束
-
每次循环,所获得元素放入循环变量,并执行一个语句块
遍历循环的应用
计数循环 (N次)
for i in range(N)
<语句块>
- 遍历由range()函数产生的数字序列,产生循环
e.g.
for i in range(5):
print(i)
计数循环 (特定次)
for i in range(M,N,K):
<语句块>
- 遍历由range()函数产生的数字序列,产生循环
e.g.
for i in range(1,6):
print (i)
结果为:
1
2
3
4
5
for i in range(1,6,2):
print("Hello:",i)
结果为:
Hello:1
Hello:3
Hello:5
字符串遍历循环
for c in s: ----变量c可以使用其他非保留字的变量名称,习惯使用c!!!!
<语句块>
- s是字符串,遍历字符串每个字符,产生循环
e.g.
for c in "Python123":
python(c,end="")
结果为:
p,y,t,h,o,n,1,2,3,
列表遍历循环
for item in ls:
<语句块>
- ls是一个列表,遍历其每个元素,产生循环
e.g.
for item in [123,"py",456]:
print(item,end=",")
结果为:
123,py,456,
文件遍历循环
for line in fi:
<语句块>
- fi是一个文件标识符,遍历其每行,产生循环
e.g.
fi = open('D:/PythonWorkplace/1.txt')
for line in fi:
print(line)
##