How to find a specific number from a user inputed list in python -


this question has answer here:

i want program find number of times particular number occurs within list. doing wrong here?

def list1():     numinput = input("enter numbers separated commas: ")     numlist = numinput.split(",")     numfind = int(input("enter number for: "))     count = 0     num in numlist:         if num == numfind:             count += 1     length = len(numlist)     # dividing how many times input number entered      # length of list find %     fraction = count / length     print("apeared",count,"times")     print("constitutes",fraction,"% of data set") list1()    

numlist isn't list of numbers, it's list of strings. try converting integer before comparing numfind.

    if int(num) == numfind: 

alternatively, leave numfind string:

numfind = input("enter number for: ") 

... although may introduce complications, e.g. if user enters 1, 2, 3, 4 list (note spaces) , 2 number, "appeared 0 time" because " 2" , "2" won't compare equal.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -