Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:\Users\dummy\Desktop\New Text Document.py", line 15, in <module>
    sum = sum + int(number)
```'
Any help would be appreciated 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading