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)
9
  • Which line throws this error?.. Commented Jul 6, 2018 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() Commented Jul 6, 2018 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() Commented Jul 6, 2018 at 13:18
  • 2
    And as spotted by @Rakesh you use the same name for the function and the variable. Commented Jul 6, 2018 at 13:19
  • 2
    @Rakesh don't remove, rename Commented Jul 6, 2018 at 13:21

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.