简体   繁体   中英

Unexpected argument passing dictionary to function with **kwargs python3.9

I'm trying to pass a dictionary to a function called solve_slopeint() using **kwargs because the values in the dictionary could sometimes be None depending on the user input. When I try to do that, I get a TypeError saying:

solve_slopeint() takes 0 positional arguments but one was given

Here's the whole process of what's happening:

  1. arg_dict values set to None and program asks a question
  2. User says yes. A function is then called and starts with another question. The user inputs 2
  3. User is asked to enter point 1 and then asked to enter point 2
  4. Those 2 points are returned to the parent function as a list of lists.
  5. arg_dict accesses those values and sets them to its corresponding keys.
  6. solve_slopeint() is called with arg_dict as the parameter.

Here is the code that you need to see:

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 to see the output of the debugger in PyCharm.

Just so people know, I left out a lot of error checking making sure that the user doesn't intentionally or accidentally input something wrong. If I left out some code that makes this code here not understandable, please tell me in the comments.

Does anyone know how to fix this?

You're wrong calling the solve_slopeint function.

Do it like this:

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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