0

Total beginner question, from what I can gather I've managed to assign a string incorrectly, but I can't see where I've gone wrong.

Any help would be appreciated, code below:

students = []

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

while 1 == 1:
try:
    add_student = input("Do you wish to enter the name of a Student (Yes/No)?")
    if add_student == "Yes":
        student_name = input("Enter student name: ")
        print(student_name)
        student_id = input("Enter student ID: ")
        print(student_id)
        print(student_name + ' ' + student_id)
        add_student(student_name, student_id)
        print(student_name+' '+student_id)
        print(*students)
    elif add_student == "No":
        break
    else:
        print("Invalid answer 1")
except KeyError:
    print("Invalid answer 2")

print(*students)
  • Which line throws this error?.. – Psytho Jul 06 '18 at 13:17
  • 4
    Remove `add_student(student_name, student_id)`... `add_student` is the input string. Or replace `def add_student` to `def add_studentSomething()` – Rakesh Jul 06 '18 at 13:17
  • 4th line, already an issue. `students` is defined outside the function and you append to it inside the function. Those are 2 separate space. If you want to keep this implementaton, you need to put `global students` at the beginning and inside the function `add_student()` – Mathieu Jul 06 '18 at 13:18
  • 2
    And as spotted by @Rakesh you use the same name for the function and the variable. – Mathieu Jul 06 '18 at 13:19
  • @Mathieu there is no need to define students as global – Igl3 Jul 06 '18 at 13:21
  • 2
    @Rakesh don't remove, **rename** – FHTMitchell Jul 06 '18 at 13:21
  • @Mathieu appending to a list outside of a function is not a problem in python – johnashu Jul 06 '18 at 13:23
  • @johnashu it's bad practise though. Functions shouldn't modify anything and methods should only modify their objects. – FHTMitchell Jul 06 '18 at 13:24
  • Thank you all for the comments, this was really helpful, I pasted the function from an exercise when I was creating my own test code & missed the name match. I've yet to cover rules around when functions and methods should modify objects, but I'll be sure to read up on it. Thanks again. – CanCanPelican Jul 06 '18 at 13:34

3 Answers3

1

You've called the function def add_student(...) and the variable add_student = .... How is the interpreter supposed to know you want the function when the variable is called that?

In fact, you've overwitten the function. By the time you call it, it no longer exists. Rename the variable or function to something else. I'll let the comments help you with certain other issues (global lists...) in your code.

FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
1

your function and local variable name same

students = []

def add_student_data(name, student_id):
    student = {"name": name, "student_id": student_id}
    students.append(student)

while 1 == 1:
try:
    add_student = input("Do you wish to enter the name of a Student (Yes/No)?")
    if add_student == "Yes":
        student_name = input("Enter student name: ")
        print(student_name)
        student_id = input("Enter student ID: ")
        print(student_id)
        print(student_name + ' ' + student_id)
        add_student_data(student_name, student_id)
        print(student_name+' '+student_id)
        print(*students)
    elif add_student == "No":
        break
    else:
        print("Invalid answer 1")
except KeyError:
    print("Invalid answer 2")

print(*students)
1

The name of the method def add_student(name, student_id) and the name of the variable add_student = input("Do you wish to enter the name of a Student (Yes/No)?") is the same.

It exist a similar question here.

You have to change the name of one of then.

students = []


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


while 1 == 1:
    try:
        user_respond = raw_input("Do you wish to enter the name of a Student (Yes/No)?")
        if user_respond == "Yes":
            student_name = raw_input("Enter student name: ")
            print(student_name)
            student_id = raw_input("Enter student ID: ")
            print(student_id)
            print(student_name + ' ' + student_id)
            add_student(student_name, student_id)
            print(student_name + ' ' + student_id)
            print(students)
        elif user_respond == "No":
            break
        else:
            print("Invalid answer 1")
    except KeyError:
        print("Invalid answer 2")

print(students)