Question:
File input.txt contains numbers separated by comma as shown below,
occurrences of a number in that file. for example,
countNum(“input.txt”,9) should return 2
countNum(“input.txt”,100) should return 0
My Code :
File input.txt contains numbers separated by comma as shown below,
Output:6,8
7,6
2,8
9,5
9,6
a. Write a function countNum(file_name,num) such that it returns number ofoccurrences of a number in that file. for example,
countNum(“input.txt”,9) should return 2
countNum(“input.txt”,100) should return 0
My Code :
def countNum(): with open("/home/aankrose/PycharmProjects/study/input.txt","r") as f: S = 0 for line in f.readlines(): line = line.strip() T = line.split(',') S += T print(T) print(line, "SUM " , S) c = countNum()
Error:Traceback (most recent call last):
File "/home/aankrose/PycharmProjects/study/phase1.py", line 11, in <module>
c = countNum()
File "/home/aankrose/PycharmProjects/study/phase1.py", line 7, in countNum
S += T
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
Please help