简体   繁体   English

Python初学者-TypeError:“ str”对象不可调用

[英]Python Beginner - TypeError: 'str' object is not callable

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)

You've called the function def add_student(...) and the variable add_student = ... . 您已经调用了函数def add_student(...)和变量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. 我将让注释帮助您解决代码中的某些其他问题(全局列表...)。

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)

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. 方法def add_student(name,student_id)的名称和变量add_student = input(“您是否要输入学生的姓名(是/否)?”)的名称是相同的。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM