#๐Ÿ”’ Why is the absolute value not working?

38 messages ยท Page 1 of 1 (latest)

rain monolith
#
def sum_nested_list(lst1):
    res = 0
    for elem in lst1:
        if type(elem) == list:
            map(abs, elem)
            res += sum_nested_list(elem)
        elif type(elem) == str:
            continue
        else:
            abs(elem)
            res += elem
    return res

print(sum_nested_list([1, 2, [-3, -4.5], 'abc', [5, 'abc', [-4, 0.5]]]))
amber hatchBOT
#

@rain monolith

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

blazing sky
#

you're mapping but not doing anything with the result

#
elem = map(abs, elem)
#
map(abs, elem)
#

@rain monolith

rain monolith
#

its not working

blazing sky
#
abs(elem)
#

same here

#
elem = abs(elem)
rain monolith
#

now its giving me an error

#

@blazing sky

blazing sky
rain monolith
#

How do i do that

rain monolith
blazing sky
#

Copy and paste it here

rain monolith
#

Ok

#

Traceback (most recent call last):
File "/Users/naderrawashdy/PycharmProjects/pythonex3/Ex3.py", line 44, in <module>
print(sum_nested_list([1, 2, [-3, -4.5], 'abc', [5, 'abc', [-4, 0.5]]]))
File "/Users/naderrawashdy/PycharmProjects/pythonex3/Ex3.py", line 36, in sum_nested_list
res += sum_nested_list(elem)
File "/Users/naderrawashdy/PycharmProjects/pythonex3/Ex3.py", line 33, in sum_nested_list
for elem in lst1:
TypeError: bad operand type for abs(): 'str'

blazing sky
#

What do you think this is telling you?

rain monolith
#

That abs doesn't work for strings

#

But im not using it on strings that's why im confused

blazing sky
#

you are using it on a string by mapping a list to abs

#
[5, 'abc', [-4, 0.5]]
#

!e

data = [5, 'abc', [-4, 0.5]]
print(*map(abs, data))
amber hatchBOT
#

@blazing sky :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 2, in <module>
003 |     print(*map(abs, data))
004 | TypeError: bad operand type for abs(): 'str'
blazing sky
#

You're nearly there with the solution, you just don't need to map your lists

#

let the else handle abs'ing

rain monolith
#

Let's gooooo

#

I got it

blazing sky
#

np, happy to help!

#

I know recursion can be super confusing

#

(I still don't really get it either, haha)

rain monolith
#

Thanks for being patient with me

rain monolith
#

!close

amber hatchBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.