Loading... ## if 语句 ``` users = ["woody", "Admin", "uni", "bob", "NEPTUNE"] if users:## 确定列表不是空的 for user in users: if user.lower() == "admin": print(f"Hello {user}, would you like to see a status report?") else: print(f"Hello {user}, thank you for logging in again.") else: print("we need to find some users") current_users = ["woody", "Admin", "uni", "bob", "NEPTUNE"] new_users = ["Nepgear", "Neptune", "Uni", "ROM", "RAM"] current_users_copy = [user.lower() for user in current_users] ## 创建副本 for user in new_users: if user.lower() in current_users_copy: print("You need to set another user name") else: current_users.append(user) print(f"Welcome {user}") numbers = list(range(1, 12)) ## 1- 10的序数 for number in numbers: if number == 1: print(f"{number}st") elif number == 2: print(f"{number}nd") elif number == 3: print(f"{number}rd") elif number < 11: print(f"{number}th") else:## else 是有风险的,可能有错误或恶意数据 print(f"invalid number {number}.") ``` 结果 ``` Hello woody, thank you for logging in again. Hello Admin, would you like to see a status report? Hello uni, thank you for logging in again. Hello bob, thank you for logging in again. Hello NEPTUNE, thank you for logging in again. Welcome Nepgear You need to set another user name You need to set another user name Welcome ROM Welcome RAM 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th invalid number 11. ``` Last modification:February 14, 2022 © Allow specification reprint Like 如果觉得我的文章对你有用,请留下评论。