Python学习笔记——if语句

本章节的内容较少,且python中条件语句的使用较为简单,故在此仅挑选了两大道较有代表性的练习题。

import random as r  #使用random模块,用于生成随机数

age=r.randint(0,100)  #5-6 if-elif-else语句的应用——根据年龄输出不同语句,这里生成了0-100的随机整数
print('age:',end='')
print(age)
if age<2:
    print('He is a baby.')
elif age>=2 and age<4:
    print('He is learning to walk.')
elif age>=4 and age<13:
    print('He is a kid.')
elif age>=13 and age<20:
    print('He is a teenager.')
elif age>=20 and age<65:
    print('He is a adult.')
else:
    print('He is an old man.')
print('\n')

users=['001','002','003','004','admin']  #5-8&9
for user in users:
    if user=='admin':  #识别目标元素
        print('Hey, ',user,'!')
    else:
        print('Hi, ',user,'!')
print('Now we clear the users list.')
users.clear()
if len(users)==0:
    print('We need to find users!')
print('\n')

2018/3/19

猜你喜欢

转载自blog.csdn.net/ltc8600/article/details/79618496