教材第五章习题
5-2
print("huan"=="huan")
print("huang"!="huang")
print("Huan".lower()=="huan")
print("Huang".lower()=="huan")
print(0.2>0.21)
print(-1<=0)
print(6>3 and 6<9)
print("huang"=="Huang" or 0.1==0.10001)
names = ["huang",0.22,'r', "Huang"]
print("huang" in names)
print(0.22 not in names)
5-7
age = 20
if age<2:
print("He is a baby.")
elif age<4:
print("He is learning to walk.")
elif age<13:
print("He is a child.")
elif age<20:
print("He is a teenager.")
elif age<65:
print("He is an adult.")
else:
print("He is an oldman.")
5-8
user_names = ["admin", "huan1", "huan2", "huan3", "huan4"]
for user_name in user_names:
if user_name == "admin":
print("Hello admin, would you like to see a status report?")
else:
print("Hello Eric, thank you for logging in again.")
5-9
user_names = ["admin", "huan1", "huan2", "huan3", "huan4"]
for user_name in user_names:
if user_name == "admin":
print("Hello admin, would you like to see a status report?")
else:
print("Hello Eric, thank you for logging in again.")
if not user_names:
print("We need to find some users.")
user_names = []
if not user_names:
print("We need to find some users.")
5-10
current_users = ["Alice", "Mike", "John", "Jack", "David"]
new_users = ["Lucy", "Lily", "Jack", "Alice", "Eric"]
for i in range(0,len(current_users)):
current_users[i] = current_users[i].lower()
for new_user in new_users:
if(new_user.lower() in current_users):
print("The user name has been used, please use another name.")
else:
print("The name has not been used yet, you can use it.")
5-11
list = [1,2,3,4,5,6,7,8,9]
for i in list:
if(i==1):
print(i,end="")
print("st")
elif(i==2):
print(i, end="")
print("nd")
elif(i==3):
print(i, end="")
print("rd")
else:
print(i,end="")
print("th")