-1

HW problem: "Create a list called some_list from -100 to 0 and then replace every even number with its positive value"

I tried the following code, but it's giving me a NoneType error, saying "'NoneType' object has no attribute 'append'" which leads me to guess that one of the answers in this loops produces a None, which I'm not sure exactly how that is happening...

some_list = []
for i in range(10):
    if i % 2 == 0:
        i = abs(i)
        some_list = some_list.append(i)
    elif i % 2 == 1:
        some_list = some_list.append(i)
    else:
        pass
some_list

I want to call some_list and get back the following:

print(some_list)
[100,-99,98,-97,96,...,0]

Many thanks!

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Does this answer your question? [Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?](https://stackoverflow.com/questions/11205254/why-do-these-list-operations-methods-clear-extend-reverse-append-sort) or [Why does x = x.append(...) not work in a for loop?](https://stackoverflow.com/questions/6339235/) – Karl Knechtel Sep 15 '22 at 01:01

1 Answers1

0

In Python, the [].append operation returns None.
So, when you run some_list = some_list.append(i) you are assigning None to the some_list variable.

print([].append.__doc__)   # L.append(object) -> None -- append object to end
Jorge F. Sanchez
  • 321
  • 4
  • 17