python

使用if语句处理列表

2018-10-29  本文已影响8人  庵下桃花仙
# 5-8
user_names = ['Li Yangjian', 'admin', 'Liuyan', 'Yang di', 'Yang lan']

for user_name in user_names:
    if user_name == 'admin':
        print("Hello " + user_name + ", would you like to see a status report?")
    else:
        print("Hello "+ user_name + ", thank you for logging in again!")

# 5-9
user_names = []

if user_names:
    for user_name in user_names:
        if user_name == 'admin':
            print("Hello " + user_name + ", would you like to see a status report?")
        else:
            print("Hello "+ user_name + ", thank you for logging in again!")
else:
    print("\nWe need to find some users!")

# 5-10
current_users = ['zhao', 'qian', 'sun', 'li', 'zhou']
new_users = ['jiang', 'shen', 'han', 'Li', 'yang']

for new_user in new_users:
    if new_user.lower() in current_users:
        print("Please enter another user name!")
    else:
        print("This user name is not used.")

# 5-11
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

if numbers:
    for num in numbers:
        if num == 1:
            print(str(num) + "st")
        elif num == 2:
            print(str(num) + "nd")
        elif num == 3:
            print(str(num) + "rd")
        else:
            print(str(num) + "th")
else:
    pirnt("No number!")
Hello Li Yangjian, thank you for logging in again!
Hello admin, would you like to see a status report?
Hello Liuyan, thank you for logging in again!
Hello Yang di, thank you for logging in again!
Hello Yang lan, thank you for logging in again!

We need to find some users!
This user name is not used.
This user name is not used.
This user name is not used.
Please enter another user name!
This user name is not used.
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
上一篇下一篇

猜你喜欢

热点阅读