python输入一个列表的语句_python自学笔记使用if语句处理列表作业
1.以特殊 方式跟管理員打招呼:創建一個至少包含5個用戶名的列表,且其中一個用戶名為‘admin’。想象你要編寫代碼,在每位用戶登陸網站后都打印一條問消息。遍歷用戶列表,并向每位用戶都打印一條問候消息。
如果用戶名為‘admin’,就打印一條特殊的問候消息,如‘Hello admin,would you like to see a status report?'否則,打印一條普通的問候消息,如“Hello Eric,thank you for longging in again"。輸入
admins=['admin','aaaa','ssss','dddd','qqqq']
for ad in admins:#for循環打印列表
if ad =='admin':#if條件語句
print("Hello admin,would you like to see a status report?")
else:
print("Hello Eric,thank you for longging in again.")
輸出
Helloadmin,would you like to see a status report?
HelloEric,thank you for longging in again.
HelloEric,thank you for longging in again.
HelloEric,thank you for longging in again.
HelloEric,thank you for longging in again.
2.處理沒有用戶的情形:在完成練習1編寫的程序中,添加一條if語句,檢查用戶列表是否為空。
如果為空,就打印消息“We need to find some users!".刪除列表中所有用戶名,確定將打印正確的消息。輸入
admins=[]
if admins:#if條件語句
for ad in admins:
if ad == 'admin':
print("Hello admin,would you like to see a status report?")
else:
print("Hello Eric,thank you for longging in again.")
else:
print("We need to find some users!")
輸出
We need to find some users!
3.檢查用戶名:按下面的說明編寫一個程序,模擬網站確保每位用戶的用戶名都獨一無二的方式。
創建一個至少包含5個用戶名的列表,并將其命名為current_users。再創建一個包含5個用戶名的列表,并將其命名為new_users,并確保其中有一兩個用戶名也包含在列表current_users中。遍歷列表new_users,對于其中的每個用戶名,都檢查它是否已被使用。如果是這樣,就打印出一條消息,指出需要輸入別的用戶名;否則,打印一條消息,指出這個用戶名未被使用。確保比較時不區分大小寫;換句話說,如果用戶名“John"已被使用,應拒絕用戶名"JOHN"。輸入
current_users=['Kol','Asd','Bss','Taa','Svv','Hsd']
new_users=['Kol','Asd','Dss','Yss','Qss']
lower_current_users = []
for current_user in current_users: lower_current_users.append(current_user.lower())#將current_users列表中全部字母小寫并添加到lower_current_users中
for new_user in new_users:
if new_user.lower() in lower_current_users:
print("已經被使用了!")
else:
print("用戶名未被使用")
輸出
已經被使用了!
已經被使用了!
用戶名未被使用
用戶名未被使用
用戶名未被使用
總結
以上是生活随笔為你收集整理的python输入一个列表的语句_python自学笔记使用if语句处理列表作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: illustrator条形码_Barco
- 下一篇: websocket python爬虫_p