How to find a specific number from a user inputed list in python
本问题已经有最佳答案,请猛点这里访问。
我想让程序找出一个特定数字在列表中出现的次数。我在这里做错什么了?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def list1(): numInput = input("Enter numbers separated by commas:") numList = numInput.split(",") numFind = int(input("Enter a number to look for:")) count = 0 for num in numList: if num == numFind: count += 1 length = len(numList) # dividing how many times the input number was entered # by the length of the list to find the % fraction = count / length print("Apeared",count,"times") print("Constitutes",fraction,"% of this data set") list1() |
1 | if int(num) == numFind: |
或者,将
1 | numFind = input("Enter a number to look for:") |
…虽然这可能会带来一些复杂的情况,例如,如果用户输入
代码有两个问题,第一个是比较
1 | numList = map(int, numInput.split(",")) |