Scientific Computing with Python Projects - Arithmetic Formatter - convert to string

I am having issues with this line of code. answerLine = str(answer) ,
The error message is TypeError: ‘list’ object is not callable
I have checked my data type of answer is integer, and I am trying to convert from int to string. I have learned in SO that my “answer” is a list, but I still don’t know how to convert it to string. Any help would be greatly appreciated.

def arithmetic_arranger(str,optional_arg = False):
  #If there are too many problems supplied to the function. The limit is five
  if len(str) > 5:
    print ("Error: Too many problems.")
  arranged_problems = []
  first_line = ""
  second_line = ""
  dashline =""
  answer = ""
  #x is the first digit and z is the second digit 
  for p in str:
    a,op,b = p.split()
    num1 = int(a)
    num2 = int(b)
    if op == "+":
      answer = int(a) + int(b)
    if op == "-":
      answer = int(a)- int(b)
      answerLine = str(answer)

  #Multiplication and division will return an error. 
    if op not in ["+", "-"]:
      print (" Error: Operator must be '+' or '-'.")
  

  
    if num1 >= 10000 or num2 >= 10000:
      print ("Error: Numbers cannot be more than four digits.")
     
    width = max(len(a), len(b)) + 2
    first_line = first_line + a.rjust(width) + "    "
    second_line = second_line + op + " " + b.rjust(width-2) + "    "
    dashline = dashline + ("-") * int(width) + "    "
    answer = answer 
    if optional_arg == True:
      arranged_problems = first_line + "\n" + second_line + "\n" + dashline + "\n" + answer
    else:
      arranged_problems = first_line + "\n" + second_line + "\n" + dashline

  return arranged_problems

That error means that str is a list. Although there’s build function str, that’s not what str points to in the code. Do you see how that happened?

1 Like

Thank you. I should not use the keyword str. I changed str to problem and the issue is fixed. Thank you for your help!