我試圖計算列表中數字的出現次數。所以基本上,我有一個列表:在用戶輸入時計算列表中出現的數字的數量
lst = [23,23,25,26,23]
和程序將首先提示用戶從列表中選擇一個數字。
"Enter target number: "
例如,如果目標是23,那麼它將打印出列表中出現多少次23。
output = 3 #since there are three 23s in the list
這裏就是我試過,它導致了一個無限循環:
lst = [23,23,24,25,23]
count = 0
i = 0
prompt= int(input("Enter target: "))
while i< len(lst)-1:
if prompt == lst[i]:
count+=1
print(count)
else:
print("The target does not exist in the list")
我不應該使用任何圖書館,所以我真的很感激,如果有人可以幫助我通過指向在我寫的代碼中找出錯誤。此外,我更喜歡'while循環'的用法,因爲我在練習使用while循環,而我至少明白這一點。
你需要一個'我+ = 1'地方。你的代碼中''i''總是'0'。 – smarx