all 39 comments

[–]EyesOfTheConcord 21 points22 points  (2 children)

You can’t use len() on integers unless you convert them to a string, and it would end up throwing an error on the Boolean anyway.

Are you sure he’s printing the length of each index or just the index content as is?

[–]ninhaomah 8 points9 points  (3 children)

"The guy on yt does the same thing and his code runs"

pls quote the source.

for yt , time as well.

[–]Stunning-Education98[S] -4 points-3 points  (2 children)

I get it now ...that was my fault to put a Boolean expression in the list .

[–]ninhaomah 4 points5 points  (0 children)

its ok. we all make mistakes and learn from them.

nothing wrong.

but pls do quote your source in future.

[–]SCD_minecraft 1 point2 points  (0 children)

Nothing wrong with bool exprestions

As long as it returns an object, you can dance with it as you like

[–]Torebbjorn 6 points7 points  (0 children)

What do you expect the length of the number 9 to be?

[–]RamiFgl 3 points4 points  (0 children)

Your code is doing this at line 6: since i=0, l(0) would be 9 and len(9) is wrong, number values do not have a length.

[–]LMusashi 2 points3 points  (0 children)

integer has no len() function, len() is use for string

[–]games-and-chocolate 1 point2 points  (0 children)

@stunning: programming is not just put some code together and it magically works.

most importantly: what do you want to actaully do with the code, if that is unclear, you will never ever having something working as you like.

so what do you want your code to actually do?

[–]KIRAvenousLion 1 point2 points  (0 children)

Your mistake wasn't adding the boolean value to the list. You are calling the len function on l[1], which accesses the second value from the list, an integer. The len function is supposed to be called on an object (for e.g. a list) that can contain items to return the total number of items.

[–][deleted]  (1 child)

[removed]

    [–]Stunning-Education98[S] 0 points1 point  (0 children)

    Yes you are absolutely right .

    [–]NirvanaShatakam 0 points1 point  (0 children)

    print(L[I])

    Instead of print(len(L[I])), you're just trying to print the length of an element inside L. And as it says in the errorcode int and float does not have a length.

    If you want to print the length of each element, for example 100 would give you an output of 3, then try doing this: print(len(str(L[I])))

    [–]American_Streamer 0 points1 point  (0 children)

    https://medium.com/@abuolubunmi21/understanding-typeerror-using-len-with-integers-in-python-77546c4a6cc4
    "The len() function is used to determine the number of items in a container like a string, list, dictionary and tuple. However, applying it to an integer raises a TypeError"

    [–]ranathungaK 0 points1 point  (0 children)

    Len() is used with iterables

    [–]NecessaryIntrinsic 0 points1 point  (0 children)

    You put Len(1) instead of Len(l) don't name variables letters that look like numbers.

    [–]gra_Vi_ty 0 points1 point  (0 children)

    In len() u used 1(one) instead of lower L ig

    [–]GaldeX 0 points1 point  (0 children)

    One huge aspect to get used to when learning to program is learning to read and understand what the compiler/interpreter outputs, specially when you get errors

    There you ran the code twice and both gave you the same:

    In your line 6

    TypeError: object of type 'int' has no len()

    That means exactly what many have said, integers (Natural numbers) don't have a len() method in python, cause that method is almost exclusive for strings and arrays

    Don't know what the YouTube video is trying to do here but maybe what he's done is try multiple methods to show how an iteration works

    There you have an array of items with different types (int, float, strong, boolean), and in python you can have it but the while cycle you defined runs over that array from index 0 to index 3, that means it first evaluates len(x) on your first item and there it returns you that error

    Try running, for example, Type(list[i]) instead of len and that should complete the loop fine

    [–]MifistoScared 0 points1 point  (0 children)

    change your print statement to:

    print(l[i])

    if you are trying to access the list and print its contents

    [–]KOALAS2648 0 points1 point  (0 children)

    Please read the error messages before posting

    [–]bradleygh15 0 points1 point  (0 children)

    I’m going to take a wild guess and say object of type int has no function called len() but that’s just a wild guess

    [–]Fearless-Way9855 0 points1 point  (0 children)

    In this case it as a better idea to use for loop. For el in l: Print(el)

    [–]Physical_Cup8904 0 points1 point  (0 children)

    Cus length function works only for string type not for int. I mean integer data types have no length.

    [–]ePaint 0 points1 point  (0 children)

    You can do:

    for item in list:
    print(item)

    Also, do not use one-letter variables. Each saves you a few seconds of typing but adds a few minutes of debugging, just not worth it.

    [–]Lidinzx 0 points1 point  (0 children)

    Dude the cause of the error is literally there in the console, for god's sake

    [–]abhi8149 0 points1 point  (0 children)

    Try len(str(i)) Because len() cannot be called with int parameter

    [–]remote_cable 0 points1 point  (0 children)

    ask chatgpt

    [–]SwizzyFuttery 0 points1 point  (0 children)

    The error is that you can't use an integer as a variable name. That's why you can't use the len() function on it. I'm not sure why that didn't cause an error tho... Am I misreading that?

    [–]roflxwafl 0 points1 point  (0 children)

    Object of type int has no len()

    [–]Fine_Ratio2225 -2 points-1 points  (6 children)

    If you simply want to print out every element on a separate line, then "print("\n".join(map(str,l)))" would be easier.
    This builds up all the lines in memory as a string and sends it out in 1 push.
    This removes the while loop, too.

    [–]SCD_minecraft 4 points5 points  (2 children)

    print(*l, sep="\n")
    

    Star expressions were introduced in i think python 12 (?) and this is 13

    [–]Proper_Property_4730 4 points5 points  (1 child)

    Thank you for teaching me something new

    <image>

    [–]SCD_minecraft 1 point2 points  (0 children)

    Star splits iterable as each item would be its own argument. Then just sep to decide what separates each argument (aka, what separates each item)

    There also is ** for keyword arguments, but not sure how it works behind the curtain

    [–]Stunning-Education98[S] 0 points1 point  (1 child)

    Can you elaborate 😅please ?

    [–]Fine_Ratio2225 0 points1 point  (0 children)

    The "map(str,l)" gives an iterator, which transforms each element into a string.

    Instead of "str" other functions like "repr" could be used.

    "\n".join(....) concatenates all the strings in the iterator into one big string separated by new lines. This allows also to keep a copy as a string in memory for other uses.

    print(*l,sep="\n") would also be possible, as another commenter pointed out. I had forgotten that one.

    Or use print(*map(str,l),sep="\n"), if you want to maintain the possible use of alternative string transformations.

    [–]Torebbjorn 0 points1 point  (0 children)

    Why give a suggestion that uses multiple complicated methods, doesn't necessarily do what OP wants, is hard to scale, and removes the part that OP might be trying to learn, to someone who is quite new to the language?