type error int object is not iterable on function python

Viewed 24
def screen_size_w(screensize: int, width: int) -> int:
   return ((sum(screensize)) - (sum(width))) / (sum(width))

def screen_size_h(screensize: int, height: int) -> int:
   return ((sum(screensize)) - (sum(height))) / (sum(height))


print(screen_size_h(1600, 500))
print(screen_size_h(2000, 500))

when I run it, it gives me the TypeError: 'int' object is not iterable, I've seen a lot of people having this problem, and a lot of solutions too, but they didn't really help a lot, I've tried everything I could think of, but I always get the same error. If you could help me in any way, I'd appreciate it

1 Answers

The sum() function takes a type iterable and returns the sum of all the elements in that iterable. When you pass it a type int, as you do here, it throws a TypeError, because an int is not an iterable.

It's probable you want something like this:

def screen_size_w(screensize: int, width: int) -> int:
   return (screensize - width) / width

sum() will give you something like this (from the REPL):

>>> ls = [1,2,3]
>>> sum(ls)
6

ls is a list in this case, an iterable. sum() moves over this list and adds up all the elements. You can see why it wouldn't make much sense to do this:

>>> sum(3)

Given the names of your functions, you're clearly trying to determine a ratio between the resolution (?) of a screen and it's width and height. It's probable you can do this math directly, without sum().

Related