if elif else
#!/usr/bin/env python3 # -*- coding: utf-8 -*- age = 20 if age >= 18: print('your age is', age) print('adult') age = 3 if age >= 18: print('your age is', age) print('adult') else: # 别忘了冒号 print('your age is', age) print('teenager') if age >= 18: print('adult') elif age >= 6: # elif是else if的缩写 print('teenager') else: print('kid') >>> birth = input('birth: ') birth: 1992 >>> if birth < 2000: ... print('00前') ... else: ... print('00后') ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() < int() >>> s = input('birth') birth 1992 >>> birth = int(s) >>> if birth < 2000: ... print('00前') ... else: ... print('00后') ... 00前 height = 1.75 weight = 80.5 bmi = weight / (height * height) if bmi <= 18.5: print('过轻') elif bmi <= 25: print('正常') elif bmi <= 28: print('过重') elif bmi <= 32: print('肥胖') else: print('严重肥胖')
for in
#!/usr/bin/env python3 # -*- coding: utf-8 -*- names = ['Michael', 'Bob', 'Tracy'] for name in names: print(name) ''' Michael Bob Tracy ''' sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + x print(sum) ''' >>> list(range(5)) [0, 1, 2, 3, 4] ''' sum = 0 for x in range(101): sum = sum + x print(sum) # 5050 #计算100以内所有奇数之和 sum = 0 n = 99 while n > 0: sum = sum + n n = n - 2 print(sum) # 2500 #练习 L = ['Bart', 'Lisa', 'Adam'] for name in L: print('Hello,', name) ''' Hello, Bart Hello, Lisa Hello, Adam '''