AttributeError: 'NoneType' object has no attribute 'append' in Python
Hello you guys, I am a newbie in Python and I'm also studying more about Python.
I have a small assignment, I want to insert some items into an array and print it out. Like this:
import numpy as np
numtasks = int(input("Enter no. of tasks today: "))
count_input = 0
tasks = []
while count_input != numtasks:
# Input task and push into array
intask = input("Input task: ")
tasks = tasks.append([intask])
# Condition & break loop
count_input = count_input + 1
if(count_input == numtasks):
break
print(tasks)
But I get an exception AttributeError: 'NoneType' object has no attribute 'append' when I run code above.
Enter no. of tasks today: 2
Input task: Learn english
Input task: Coding
Traceback (most recent call last):
File "main.py", line 11, in <module>
tasks = tasks.append([intask])
AttributeError: 'NoneType' object has no attribute 'append'
And I am using python 3.8.2
Anybody can explain it to me? How can I solve it?
Thanks for any response.
-
T1
Tuấn Nguyễn Trương Anh Sep 03 2021
You need to know
list.append()
returnsNone
so you can't assign value for an array by waytasks = tasks.append([intask])
.You can easily fix this issue by replacing
tasks = tasks.append([intask])
totasks.append([intask])
.import numpy as np numtasks = int(input("Enter no. of tasks today: ")) count_input = 0 tasks = [] while count_input != numtasks: # Input task and push into array intask = input("Input task: ") tasks.append([intask]) # Condition & break loop count_input = count_input + 1 if(count_input == numtasks): break print(tasks)
#Output
Enter no. of tasks today: 2 Input task: Coding Input task: Reading [['Coding'], ['Reading']]
I hope it helpful for you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.