I get an int error when trying to run a simple addition calculator i made in python

Question:

When trying to run this simple calculator with only addition and passing "1 + 2" in the terminal without quotes

numbers=["1","2","3","4","5","6","7","8","9"]
str=input()
numlist = str.split()
for i in range(len(numlist)):
    number=""
    operate=""
    for j in numlist[i]:
        if j in numbers:
            number=number+j
        else:
            operate=operate+j
    if operate == "+":
        sum = 0
        for i in range(len(numlist)):
            sum = sum + int(number)

it results In this

Exception has occurred: ValueError
invalid literal for int() with base 10: ''
  File "C:UsersdummyDesktopNew Text Document.py", line 15, in <module>
    sum = sum + int(number)
```'
Any help would be appreciated 
Asked By: John Christopher

||

Answers:

Each time you iterate through numlist you are resetting number to an empty string. I’d suggest moving that and sum above your for loop to make sure they are not reset on each iteration.

numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
given_input = input()
numlist = given_input.split()
number = ""
operate = ""
sum = 0
for i in numlist:
    for j in i:
        if j in numbers:
            number = j
        else:
            operate = j
    if operate == "+" and number != "":
        sum += int(number)
        number = ""
print(sum)
Answered By: Brady R
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.