# 5-3 外星人颜色#1
alien_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 外星人颜色#2
alien_color = 'yellow'
if alien_color == 'green':
print('You get 5 point.')
else:
print('You get 10 point.')
# 5-5 外星人颜色#3
alien_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-1
admins = [] # 5-9-2 重复5-9-1的代码
# 5-10 检查用户名
current_users = ['kain', 'chow', 'lily', 'zhou', 'zhang'] # 5-10-1
new_users = ['Kain', 'lily', 'Jie', 'peng', 'tj'] # 5-10-2
for 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')
Python编程:从入门到实践(课后习题5)
最新推荐文章于 2024-10-12 14:28:03 发布