Matthew Schell
Matthew Schell

Reputation: 679

Why am I getting a type error when trying to join a list to a string?

I'm trying to build a linear equation calculator with Python. After gathering information by asking the user questions, I start calculating the equation. If the user says that they have 2 points that the line goes through, I use that info to create an equation. The different parts of the equation (e.g slope, y-intercept, operations, x and y) are stored in a list. After calculating the equation, I want to join that list into a single string; However, I get a type error. Here's part of the traceback:

File "/Users/matthewschell/PycharmProjects/Linear Equation Calculator/main.py", line 19, in slope_intercept
    final_equation = solve_slopeint(**arg_dict)
  File "/Users/matthewschell/PycharmProjects/Linear Equation Calculator/solve_functions.py", line 52, in solve_slopeint
    final_equation = ''.join(equation)
TypeError: sequence item 0: expected str instance, int found

And here is the full code for the function to calculate the equation:

def solve_slopeint(**kwargs):
    if kwargs['point1'] is not None and kwargs['point2'] is not None:
        equation = [None, '= ']
        point1 = kwargs['point1']
        point2 = kwargs['point2']
        slope = (point2[1] - point1[1]) / (point2[0] - point1[0])
        slope = str(slope)

        if slope[-2:] == ".0":
            slope = str(int(float(slope)))
            num = 0  # Variable that lets program know the type of num the slope is. 0 = whole num and 1 = fraction
        else:
            num = point2[1] - point1[1]
            denom = point2[0] - point1[0]
            common_divisor = gcd(num, denom)
            numerator = num / common_divisor
            denominator = denom / common_divisor
            slope = [int(numerator), int(denominator)]
            num = 1

        equation.append(slope)
        equation.append(None)
        equation.append('+ ')
        equation.append(0)
        equation[0] = point1[1]
        equation[3] = point1[0]

        if num == 0:
            check = slope * equation[3]
            while check + equation[5] != equation[0]:
                if check + equation[5] < equation[0]:
                    equation[5] += 1

                elif check + equation[5] > equation[0]:
                    equation[5] -= 1

        else:
            check = (slope[0] * equation[3]) / slope[1]
            while check + equation[5] != equation[0]:
                if check + equation[5] < equation[0]:
                    equation[5] = equation[0] - (check + equation[5])

                elif check + equation[5] > equation[0]:
                    equation[5] = (check + equation[5]) - equation[0]

        for item in equation:
            item = str(item)
            print(item)

        final_equation = ''.join(equation)
        return final_equation

Does anyone know why I'm getting a type error?

Upvotes: 1

Views: 60

Answers (1)

notverysmart
notverysmart

Reputation: 36

In this part of the snippet:

        for item in equation:
            item = str(item)
            print(item)

You are only str()ing the new items that exist in the loop, but not saving them in the equation

        newequation = []
        for item in equation:
            newequation.append(str(item))
        equation = newequation

There's probably a cleaner way to fix this, but I'm not very good

Upvotes: 1

Related Questions