第5章 if语句

------------恢复内容开始------------

5-3外星人颜色#1:

#5-3
alien_color ='green' if alien_color =='green': print('Your get 5 points')

  

#5-3
alien_color ='yellow' if alien_color =='green': print('Your get 5 points')

5-4外星人颜色#2:

#5-4
alien_color ='green' if alien_color =='green': print('Your get 5 points') else: print('Your get 10 points')

  

#5-4
alien_color ='yellow' if alien_color =='green': print('Your get 5 points') else: print('Your get 10 points')

5-5外星人颜色#3:

#5-5
alien_color ='green'
if alien_color =='green':
    print('Your get 5 points')
elif  alien_color =='yellow':
    print('Your get 10 points')
else:
    print('Your get 15 points')

5-6人生的不同阶段:

#5-6
age = 20
if age < 2:
    print('He is baby')
elif age < 4:
    print('He is toddlering')
elif age <13:
    print('He is children')
elif age <20:
    print('He is teenagers')
elif age <65:
    print('He is adult')
else:
    print('He is oldman')

5-7喜欢的水果:

#5-7
favorite_fruits =['apple','cheery','banana']
if 'apple' in favorite_fruits:
    print('You really like apple')
elif  'cheery' in favorite_fruits:
    print('You really like cheery')
elif 'banana' in favorite_fruits:
    print('You really like banana')

5-8 以特殊方式跟管理员打招呼:

#5-8
users =['admin','dog','cat','monkey','mouse']
for user in users:
    if user =='admin':
        print('Hello admin ,would you like to see a status report?')
    else:
        print('Hello '+user+' thank you for logging in again')

5-9处理没有用户的情形:

#5-9
users =[]
if users:
    for user in users:
        if user =='admin':
            print('Hello admin ,would you like to see a status report?')
        else:
            print('Hello '+user+' thank you for logging in again')
else:
    print('We need to find some users!')

5-10检查用户名:

#5-10
current_users = ['admin','cat','dog','monkey','bird']
new_users = ['admin','tom','tim','dog','lucy']
for new_user.lower() in new_users:
    if new_user in current_users:
        print(new_user+' 已被使用,请输入别的用户名')
    else:
        print(new_user+' 该用户名未被使用')
        

5-11序数:

#5-11
lists =[1,2,3,4,5,6,7,8,9]
print(lists)
for list in lists:
    if list == 1:
        print(str(list)+'st')
    elif list == 2:
        print(str(list)+'nd')
    elif list == 3:
        print(str(list)+'rd')
    else:
        print(str(list)+'th')

 

 
posted @ 2020-05-19 15:50  猫山思  阅读(198)  评论(0)    收藏  举报