python中if条件语句和循环语句(for...in... , while)
一:if条件语句
二: 循环
1.for…in…循环
2.while 循环
三:if条件语句和循环语句交叉使用
四:扩展:循环中的break和continue
1.break
2.continue
################################################################
一:if条件语句
height = int(input(‘请输入您的身高:’))/100
weight = int(input(‘请输入您的体重:’))
bmi = weight/(height*height)
if bmi < 18.5:
print(‘过轻’)
print(’%.2f’%bmi)
elif bmi < 25:
print(‘正常’)
print(’%.2f’%bmi)
elif bmi < 28:
print(‘过重’)
print(’%.2f’%bmi)
elif bmi < 32:
print(‘肥胖’)
print(’%.2f’%bmi)
else:
print(‘严重肥胖’)
print(’%.2f’%bmi)
二: 循环
for…in…循环
2.1.1
sum = 0
for x in range(101):
sum = sum + x
print(sum)
2.1.2
L = [‘Bart’, ‘Lisa’, ‘Adam’]
for x in L:
print(‘hello %s !!!’%x)
while 循环
2.2.1
sum = 0
n = 99
while n > 0:
sum = sum + n
n = n-2
print(sum,n)
三:if条件语句和循环语句交叉使用
n = 99
sum = 0
while n > 0:
sum = sum + n
n = n-1
if sum == 2100:
print(‘sum:%s’%sum + ’ n:%s’%n)
break
print(‘end’)