Python Forum

Full Version: please help me TypeError: unsupported operand type(s) for +=: 'int' and 'list'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Question:
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 of
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 :
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
hi,

at line 5, when you use split, you create a list of two elements. the type of those elements is string.
and in your code, I don't understand what "S" count. you never compare any number to another to know if there is in a line or not. I think you need to do that.

I hope that's gonna helpful.
Lines #3 and #6:

S = 0                    # S is int
T = line.split(',')      # T is list
str.split docstring: "Return a list of the words in the string, using sep as the delimiter string."

You can't sum int and list (as error message says).

However, if you ever actually need to perform this operation then with numpy array it's quite easy:

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> a + 1
array([2, 3, 4])

Write a function countNum(file_name,num) such that it returns number of
occurrences of a number in that file. for example,
countNum(“input.txt”,9) should return 2
countNum(“input.txt”,100) should return 0


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()
row # 1: where are required parameters file_name and num?
row # 2: you should use parameter file_name, not hardcoded path
row # 4: you can iterate directly over file object (for line in f)
row # 6: keep in mind that you are splitting string, result is list of strings ['6', '8'] not ints
rows # 8 and # 9: assignment says that function should return not print

It is somewhat unclear what this code suppose to do. Can you explain in spoken language what this function does?

As sidenote - function names should be lowercase separated by underscore (PEP8 - Function and Variable Names). So count_num would be nice, readable and pythonic Smile