python编程 从入门到实践 第四章 课后习题(5-3~5-11)

#     动手试一试
#  5-3 外星人的颜色#1
#alien_color=['green','yellow','red']
alien_color='green'
if alien_color=='green':
    print("5 points get!")
alien_color='red'
if alien_color=='green':
    print("5 points get!")
#5-4 外星人颜色#2
alien_color='green'
if alien_color=='green':
    print("5 points get!")
else:
    print("10 points get!")
alien_color='red'
if alien_color=='green':
    print("5 points get!")
else:
    print("10 points get!")
# 5-5 外星人颜色#3
alien_color='red'
if alien_color=='green':
    print("5 points get!")
elif alien_color=='yello':
    print("10 points get!")
else:
    print("15 points get!")
alien_color='green'
if alien_color=='green':
    print("5 points get!")
elif alien_color=='yello':
    print("10 points get!")
else:
    print("15 points get!")
alien_color='yello'
if alien_color=='green':
    print("5 points get!")
elif alien_color=='yello':
    print("10 points get!")
else:
    print("15 points get!")
# 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 = 2
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 'pear' in favorite_fruits:
    print('You really like ' + 'pear!')
if 'peach' in favorite_fruits:
    print('You really like ' + 'peach!')

#  动手试一试
#5-8 以特殊方式跟管理员打招呼
users=['admin','amy','sam','daming','lingling']
for user in users:
    if user=='admin':
        print("hello admin,would you like to see a report?")
    else:
        print("hello eric,thank you for logging again.")
#5-9 处理没有用户的情形
users=[]
#users=['admin','amy','sam','daming','lingling']
if users:
    for user in users:
        if user=='admin':
            print("hello admin,would you like to see a report?")
        else:
            print("hello eric,thank you for logging again.")
else:
    print("we need some users.")
#5-10 检查用户名
current_users=['admin','amy','sam','daming','lingling']
new_users=['feifei','daming','qiqi','beibei','AMY']
for new_user in new_users:
    if new_user.lower() in current_users:
        print("please enter other name.")
    else:
        print("the name was not used.")
## 注意了!对于比较时不区分大小写,即AMY==amy
##  要使用new_user.lower(),,,,,,刚学过去吧,我这个菜鸟又忘了

GOOD LUCK!!!

你可能感兴趣的:(Python)