2014-02-17 55 views
-2

到目前爲止,我有...如何使用用戶輸入搜索列表?

def main(): 
     list = [8, 25, 10, 99, 54, 3, 61, 24] 
     print("Your list is: ",list) 
     new = input("Please choose a number from the list:") 
     print("Your numbers index is:", list.index(new)) 
     input("Press enter to close program") 

所以我的問題是,我無法弄清楚如何利用用戶輸入,並獲得用戶的首選指標。例如,如果用戶輸入99,我的程序將返回「您的號碼索引爲:3」

請幫忙。

+0

使用'int()'......... – devnull

回答

0

input函數返回一個字符串,而您的列表包含ints。你需要將字符串轉換爲int:

new = int(input("Please choose a number from the list: ")) 
+0

我也試過了。我不斷收到語法錯誤:無效的語法。 – Dizzy

+0

的語法錯誤,你使用Python 2.7嗎? – lessthanl0l

+0

不,我有Python 3.3 – Dizzy

0

我仔細檢查了下面的代碼。你可以嘗試運行它嗎?

def main(): 
    list = [8, 25, 10, 99, 54, 3, 61, 24] 
    print("Your list is: ", list) 
    new = int(input("Please choose a number from the list:")) 
    print("Your numbers index is:", list.index(new)) 
    input("Press enter to close program") 

main() 
+0

我知道它的工作。感謝您的全力幫助 – Dizzy

+1

如果其中一個答案幫助了您,請考慮提升或接受這些答案。否則,請回答您如何解決問題,以便其他人可以從此問題中學習 – Dannnno