简体   繁体   English

Python3 TypeError:接受1个位置参数,但给定2个

[英]Python3 TypeError: takes 1 positional argument but 2 were given

I'm new to programming, here is my code.. But I get error, attached... Please help me.. 我是编程的新手,这是我的代码。但是,我收到错误提示,请附加...。

students = []

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

name = input("Enter student name: ")
student_id = input("Enter student ID: ")

def save_file(student):
    try:
        f = open("students.txt", "a")
        f.write(student + "\n")
        f.close()
    except Exception:
        print("Could not save file")

add_student(name, student_id)
save_file(name, student_id)

Your save_file method takes a variable called student but you pass in name and student_id . 您的save_file方法采用一个名为student的变量,但您传入namestudent_id So, your method expects one argument but got two. 因此,您的方法需要一个参数,但有两个参数。 Ergo your error. 犯错。

You can modify the method to take both the name and ID by adding another argument, similar to what you did with add_student . 您可以通过添加另一个参数来修改方法以同时使用名称和ID,这与您使用add_student I would also advise that you look at the stack trace and try to understand what's going on before you ask questions. 我也建议您在问问题之前先查看堆栈跟踪并尝试了解发生了什么。 You'll learn more that way and you may come to understand your problem without seeking help. 您将通过这种方式学到更多,并且可能无需寻求帮助即可了解您的问题。

The function save_file() is allowed to get only one argument student , but you are passing two arguments name, student_id to the function. 函数save_file()仅允许获取一个参数student ,但是您正在向函数传递两个参数name, student_id That's the error! 那是错误!

Change def save_file(student) as def save_file(student, student_id): to fix the error. def save_file(student)更改为def save_file(student, student_id):以修复错误。

Hope this helps! 希望这可以帮助! Cheers! 干杯!

暂无
暂无

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

相关问题 Python tqdm TypeError: <lambda> ()接受1个位置参数,但给出了2个 - Python tqdm TypeError: <lambda>() takes 1 positional argument but 2 were given 类型错误:函数需要 1 个位置参数,但 2 个被赋予了 kivy python - TypeError: Function takes 1 positional argument but 2 were given kivy python 类型错误:__init__() 需要 1 个位置参数,但给了 2 个 Python - TypeError: __init__() takes 1 positional argument but 2 were given Python Python 继承 - 类型错误:__init__() 采用 1 个位置参数,但给出了 4 个 - Python Inheritance - TypeError: __init__() takes 1 positional argument but 4 were given TypeError:method()接受1个位置参数,但给出2个(Python和Kivy) - TypeError: method()takes in 1 positional argument but 2 were given (Python and Kivy) Python Selenium TypeError:__init__() 需要 1 个位置参数,但给出了 2 个 - Python Selenium TypeError: __init__() takes 1 positional argument but 2 were given 类型错误:wrapper() 采用 1 个位置参数,但给出了 2 个 - TypeError: wrapper() takes 1 positional argument but 2 were given TypeError:done()接受1个位置参数,但给出了2个 - TypeError: done() takes 1 positional argument but 2 were given TypeError:isnull()接受1个位置参数,但给出了2个 - TypeError: isnull() takes 1 positional argument but 2 were given TypeError: start() 接受 1 个位置参数,但给出了 2 个 - TypeError : start() takes 1 positional argument but 2 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM