我试图计算列表中数字的出现次数。所以基本上,我有一个列表:在用户输入时计算列表中出现的数字的数量
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