美文网首页python
使用if语句处理列表

使用if语句处理列表

作者: 庵下桃花仙 | 来源:发表于2018-10-29 21:54 被阅读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

相关文章

网友评论

    本文标题:使用if语句处理列表

    本文链接:https://www.haomeiwen.com/subject/sjwitqtx.html