Python Forum
Error is function call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error is function call
#1
How to resolve the before error

students = {};


def add_student(name, student_id=332) :
    student = {"name": name, "student_id": student_id}
    students.append(student);



add_student("Rahul", 12);
Error:
Traceback (most recent call last): File "F:/Python_Projects/first.py", line 11, in <module> add_student("Rahul", 12); File "F:/Python_Projects/first.py", line 7, in add_student students.append(student); AttributeError: 'dict' object has no attribute 'append' Process finished with exit code 1
Reply
#2
As it says, you can't append to a dict, append is for lists, typically. It's not clear what your target data structure is here. If you do want a list of students, change the first line to students = []. If you want a dictionary of students that you can retrieve by student ID, I would replace the body of add_student with students[id] = name.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Quote:
Error:
AttributeError: 'dict' object has no attribute 'append'

You can resolve the error by changing your data type to be something that has an append method, or changing your method to be something that exists in a dict.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can you call a function from a list? KEYS 20 6,155 Nov-10-2020, 06:36 PM
Last Post: KEYS
  Programming (identifier, literal and function call) ledangereux 5 4,961 May-05-2020, 12:37 PM
Last Post: gumi543
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,201 Jul-01-2018, 01:17 AM
Last Post: juliabrushett
  Function call Antonio_Gallardo 3 3,183 Dec-29-2017, 11:13 PM
Last Post: Larz60+
  syntax error, and call functions. Ayiden 3 4,176 Mar-23-2017, 03:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020