Why adding int into a string?

I’m doing a little program that’s essentially a calculator. Basically I wrote a function that splits the equation into a numbers and indicators and puts them into a list. I’m trying to implement parentheses support into this function, but I’m having a problem. Here’s the code:

def split(str):
  operation = str + 'p'
  tempnumstr = ''
  split_equation = []
  for i in operation.replace(' ', ''):
    if i.isnumeric() == True:
      tempnumstr += i
    else:
      tempnumint = int(tempnumstr)
      split_equation.append(tempnumint)
      split_equation.append(i)
      tempnumstr = ''

  while '' in split_equation:
    split_equation.remove('')
  while ' ' in split_equation:
    split_equation.remove(' ')
  while 'p' in split_equation:
    split_equation.remove('p')
  return split_equation

The problem I’m having is that if I try to input a str with parenthesis right now it gives me an error in the

tempnumint = int(tempnumstr)

For some reason, the program is putting something in that string that isn’t an integer and I don’t know why because it should only ever add something if “i” is numeric, that’s what

if i.isnumeric() == True:

is for. How can I fix this?

Hi Pattydaone,

“How can I fix it?”

The best way to fix bugs in your code is to start by reading the error messages that you get. When you run

tempnumint = int(tempnumstr)

and get an error, if you are using a recent version of Python, the error will tell you exactly why it failed.

So please show us the full error traceback you receive, by copying and pasting the text into your reply. (Not as a screenshot or picture.) Everything from the line that starts “Traceback” to the final exception and error message.

Even if you are using an older version of Python where the error message is less helpful, it may help a bit.

By the way, not everything that is numeric can be converted into an int. Run this code and see what happens:

c = chr(188)
print(c, c.isnumeric())
print(int(c))

The required test is isdecimal(), not isnumeric(). The default constructor for int() converts a decimal representation of an integer. Unicode currently has 66 sets of decimal digits:

>>> decimals = [c for i in range(sys.maxunicode) if (c:=chr(i)).isdecimal()]
>>> divmod(len(decimals), 10)
(66, 0)
>>> sum(int(c) for c in decimals) == sum(range(1,10)) * 66
True

Here’s the error it gives me:

ValueError: invalid literal for int() with base 10: ''

From what I’m gathering from this error, the program is adding '' to tempnumstr which causes the conversion from a string to an integer to fail. I just don’t know why since it’s only supposed to add something to the string when it passes the isnumeric() test.

tmpnumstr starts off with ‘’ as the assigned value (in line 3). So if nothing is ever appended before it is passed to int(), this will happen.

Looks to me that if the first if (on line 6) fails, then nothing is appended but it’s still passed to int().

2 Likes

Again, isnumeric() is the wrong test. You should be using isdecimal(). Unicode currently has 1872 numeric characters. Of those 660 are decimal characters that can be passed to the default int() constructor. That leaves 1212 numeric characters that cause int() to raise a ValueError. That said, the bug in your code that was pointed out by BowlOfRed is the more likely problem.

Looks like that was it. Added a try except block to circumvent that. Thank you very much.