Python Forum
please help me TypeError: unsupported operand type(s) for +=: 'int' and 'list'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help me TypeError: unsupported operand type(s) for +=: 'int' and 'list'
#1
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
Reply
#2
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.
Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Type Error: Unsupported Operand jhancock 2 1,183 Jul-22-2023, 11:33 PM
Last Post: jhancock
  search a list or tuple for a specific type ot class Skaperen 8 1,931 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,325 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,878 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,170 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,872 May-07-2022, 08:07 AM
Last Post: menator01
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,582 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,009 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,410 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,063 Feb-09-2021, 05:40 PM
Last Post: soft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020