意外的参数将字典传递给 **kwargs python3.9 函数
Unexpected argument passing dictionary to function with **kwargs python3.9
我正在尝试使用 **kwargs 将字典传递给名为 solve_slopeint() 的函数,因为根据用户输入,字典中的值有时可能是 None。当我尝试这样做时,我得到一个 TypeError 说:
solve_slopeint() takes 0 positional arguments but one was given
下面是整个过程:
- arg_dict 值设置为 None 并且程序提出问题
- 用户说是。然后调用一个函数并从另一个问题开始。用户输入 2
- 要求用户输入点 1,然后要求输入点 2
- 这 2 个点作为列表的列表返回给父函数。
- arg_dict 访问这些值并将它们设置为相应的键。
- solve_slopeint() 以 arg_dict 作为参数调用。
这是您需要查看的代码:
main.py
def slope_intercept():
arg_dict = {
"point1": None,
"point2": None,
"slope": None,
"y-intercept": None
}
while True:
question1 = input("Were you given any points that the line passes through? (y/n): ")
if question1 == 'y':
point_list = passing_points()
if len(point_list) == 2:
arg_dict["point1"] = point_list[0]
arg_dict["point2"] = point_list[1]
solve_slopeint(arg_dict)
functions.py
def passing_points():
while True:
num_points = input("How many points were you given?: ")
try:
num_points = int(num_points)
elif num_points == 2:
point1_list = []
point2_list = []
while True:
point1 = input("Enter point 1 in the format x,y: ")
while True:
point2 = input("Enter point 2 in the format x,y: ")
return [point1_list, point2_list]
solve_functions.py
def solve_slopeint(**kwargs):
print("Equation solved!")
Click Here 在PyCharm中查看调试器的输出。
为了让人们知道,我省略了很多错误检查以确保用户不会有意或无意地输入错误内容。如果我遗漏了一些代码使这里的代码无法理解,请在评论中告诉我。
有人知道如何解决这个问题吗?
你调用 solve_slopeint
函数是错误的。
这样做:
def slope_intercept():
arg_dict = {
"point1": None,
"point2": None,
"slope": None,
"y-intercept": None
}
while True:
question1 = input("Were you given any points that the line passes through? (y/n): ")
if question1 == 'y':
point_list = passing_points()
if len(point_list) == 2:
arg_dict["point1"] = point_list[0]
arg_dict["point2"] = point_list[1]
solve_slopeint(**arg_dict)
# Or:
# solve_slopeint(point1=point_list[0], point2=point_list[1])
我正在尝试使用 **kwargs 将字典传递给名为 solve_slopeint() 的函数,因为根据用户输入,字典中的值有时可能是 None。当我尝试这样做时,我得到一个 TypeError 说:
solve_slopeint() takes 0 positional arguments but one was given
下面是整个过程:
- arg_dict 值设置为 None 并且程序提出问题
- 用户说是。然后调用一个函数并从另一个问题开始。用户输入 2
- 要求用户输入点 1,然后要求输入点 2
- 这 2 个点作为列表的列表返回给父函数。
- arg_dict 访问这些值并将它们设置为相应的键。
- solve_slopeint() 以 arg_dict 作为参数调用。
这是您需要查看的代码:
main.py
def slope_intercept():
arg_dict = {
"point1": None,
"point2": None,
"slope": None,
"y-intercept": None
}
while True:
question1 = input("Were you given any points that the line passes through? (y/n): ")
if question1 == 'y':
point_list = passing_points()
if len(point_list) == 2:
arg_dict["point1"] = point_list[0]
arg_dict["point2"] = point_list[1]
solve_slopeint(arg_dict)
functions.py
def passing_points():
while True:
num_points = input("How many points were you given?: ")
try:
num_points = int(num_points)
elif num_points == 2:
point1_list = []
point2_list = []
while True:
point1 = input("Enter point 1 in the format x,y: ")
while True:
point2 = input("Enter point 2 in the format x,y: ")
return [point1_list, point2_list]
solve_functions.py
def solve_slopeint(**kwargs):
print("Equation solved!")
Click Here 在PyCharm中查看调试器的输出。
为了让人们知道,我省略了很多错误检查以确保用户不会有意或无意地输入错误内容。如果我遗漏了一些代码使这里的代码无法理解,请在评论中告诉我。
有人知道如何解决这个问题吗?
你调用 solve_slopeint
函数是错误的。
这样做:
def slope_intercept():
arg_dict = {
"point1": None,
"point2": None,
"slope": None,
"y-intercept": None
}
while True:
question1 = input("Were you given any points that the line passes through? (y/n): ")
if question1 == 'y':
point_list = passing_points()
if len(point_list) == 2:
arg_dict["point1"] = point_list[0]
arg_dict["point2"] = point_list[1]
solve_slopeint(**arg_dict)
# Or:
# solve_slopeint(point1=point_list[0], point2=point_list[1])