Python编程:从入门到实践(课后习题5)

来源:互联网 发布:嵌套fragment数据交互 编辑:程序博客网 时间:2024/05/18 00:05
# 5-3 外星人颜色#1alien_color = 'green'if alien_color == 'green':print('You get 5 point.')alien_color = 'yellow'if alien_color == 'green':print('You get 5 point.')# 5-4 外星人颜色#2alien_color = 'yellow'if alien_color == 'green':print('You get 5 point.')else:print('You get 10 point.')# 5-5 外星人颜色#3alien_color = 'red'if alien_color == 'green':print('You get 5 point.')elif alien_color == 'yellow':print('You get 10 point.')else:print('You get 15 point.')# 5-6 人生的不同阶段age = int(input('Enter your age: '))if age < 2:print('You are baby.')elif age >=2 and age < 4:print('You are toddler.')elif age >=4 and age < 13:print('You are child.')elif age >=13 and age < 20:print('You are teenager.')elif age >=20 and age < 65:print('You are adult.')elif age >= 65:print('You are elder.')else:print('Your must enter a positive interger!')# 5-6 喜欢的水果favorite_fruits = ['bananas', 'apple', 'orange']if 'bananas' in favorite_fruits:print('You really like ' + 'bananas!')if 'apple' in favorite_fruits:print('You really like ' + 'apple!')if 'orange' in favorite_fruits:print('You really like ' + 'orange!')if 'mango' in favorite_fruits:print('You really like ' + 'mango!')if 'pear' in favorite_fruits:print('You really like ' + 'pear!')# 5-8 以特殊方式跟管理员打招呼admins = ['admin', 'zhou', 'zhang', 'liu', 'li']for admin in admins:if admin == 'admin':print("Hello admin, would you like to see a status report?")else:print("Hello " + admin + ", thank you for logging in again.")# 5-9 处理没有用户的情形if admins:for admin in admins:if admin == 'admin':print("Hello admin, would you like to see a status report?")else:print("Hello " + admin + ", thank you for logging in again.")else:print("We need to find some users!")  # 5-9-1admins = []  # 5-9-2 重复5-9-1的代码# 5-10 检查用户名current_users = ['kain', 'chow', 'lily', 'zhou', 'zhang']  # 5-10-1new_users = ['Kain', 'lily', 'Jie', 'peng', 'tj']  # 5-10-2for new_user in new_users:if new_user.lower() in current_users:print("You need enter another username!")else:print("This username is not used.")# 5-11 序数list1 = list(range(1, 10))list2 = [x for x in range(1, 10)]for i in list1:if i == 1:print(str(i) + 'st')elif i == 2:print(str(i) + 'nd')else:print(str(i) + 'th')