'TypeError: 'int' object is not iterable - When trying to convert a string to a list

My code is as follows down below. However the x that I input into this function is not int data type, it should be a string. The other code will show you where the input comes from.

def validExpression(x):
  x_array = list(x)
  operators = {"+","-","*","/"}
  nums = {"0","1","2","3","4","5","6","7","8","9"}
  if x_array[0] != "(":
      return 1
  elif x_array[len(x_array)-1] != ")":
      return 1
  count = 1
  front_b_count = 1
  back_b_count = 1
  op_count = 0
  while count < (len(x_array)-2):
      if x_array[count] == "(":
          front_b_count += 1
          count += 1
      if x_array[count] == ")":
          back_b_count += 1
          count += 1
      if x_array[count] in nums:
          count += 1
      if x_array[count] in operators:
          op_count += 1
          count += 1
  if front_b_count != back_b_count:
      return 1
  if op_count > front_b_count:
      return 2
  if op_count < front_b_count:
      return 3
  else:
      return 4

The input for the function comes from this segment

expression = input("Please enter an expression: ")
    check = validExpression(expression)

So an input for expression would be something like (3+1) for example.

Any help appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source