2015-11-04 72 views
1

我無法通過用戶輸入展開我的擴展列表。我想我錯過了如何使用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 
+0

追加驗證之前沒有進入榜單 – vks

回答

2

的幾個注意事項:

  • 轉換測試成績int

  • list.append回報None,不要把它分配給什麼;使用scoreLst[-1]代替score1

  • 不使用list.remove刪除列表的最後一個元素,list.pop()會很好的工作

  • lst1 == False是比較lst1 = False是分配

  • 你創建無限循環和break一旦用戶輸入-999,我看不到需要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 
+0

後,這個偉大工程,更簡單。出於好奇,我必須在下面分配list.pop生活嗎? ( - ) break –

+0

@AndrewBodin'list.pop()'刪除列表的最後一個元素,最後一個元素是-999(我們特別在'scoreLst [-1] == -999 '),所以不,你不必做任何事情。 – vaultah

+0

順便說一句,如果你覺得我的回答對你有幫助,你可以[接受我的回答](http://meta.stackexchange.com/a/5235)。 – vaultah