[英]Breaking out of user inputs in a list
我在使用用戶輸入來擴展列表時遇到麻煩。 我想我丟失了如何使用if
語句來查詢特定項目的列表。 我需要該列表以在用戶輸入-999時要求輸入。 我還需要從列表中排除-999。 你能幫助我嗎?
print(scoreLst)
可以測試並查看它在我使用時的工作方式。
scoreLst =[]
score = ()
lst1 = True
print("The list ends when user inputs -999")
scoreLst.append(input("Enter the test score: "))
while lst1 == True:
score1 = scoreLst.append(input("Enter another test score: "))
print(scoreLst)
if score1 != -999:
lst1 == True
else:
scoreLst.remove(-999)
lst1 == False
一些注意事項:
將考試成績轉換為int
list.append
返回None
,不要將其分配給任何東西; 使用scoreLst[-1]
代替score1
不要使用list.remove
刪除列表的最后一個元素, list.pop()
會很好地工作
lst1 == False
是比較, lst1 = False
是賦值
您將創建一個無限循環並在用戶輸入-999時break
,我認為不需要lst1
最終結果:
scoreLst = []
print("The list ends when user inputs -999")
scoreLst.append(int(input("Enter the test score: ")))
while True:
scoreLst.append(int(input("Enter another test score: ")))
if scoreLst[-1] == -999:
scoreLst.pop()
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.