Write a program that calculates the sum of three numbers entered as a character string. All numbers are integers.

Example:

Enter the expression:

12+3+45

Answer: 60

–1 vote

# Store input numbers

num1 = input('Enter first number: ')

num2 = input('Enter second number: ')

num3 = input('Enter third number :')

# Add three numbers

sum = float(num1) + float(num2) +float(num3)

# Display the sum

print('The sum of {0},{1} {2} is {3}'.format(num1, num2, num3, sum))

num1 = input('Enter first number: ')

num2 = input('Enter second number: ')

num3 = input('Enter third number :')

# Add three numbers

sum = float(num1) + float(num2) +float(num3)

# Display the sum

print('The sum of {0},{1} {2} is {3}'.format(num1, num2, num3, sum))

0 votes

Hello there. You can use eval() function for your question.

expression = "12+3+45"

print(eval(expression))

Output: 60.

expression = "12+3+45"

print(eval(expression))

Output: 60.

commented
Apr 9, 2020
by
DariaSNT
(130 points)

thanks a lot!

0 votes

num = input("Enter the expression : \n")

print("Answer:",eval(num))

output:

Enter the expression :

12+3+45

Answer: 60

0 votes

I know the "eval" function is made for this, but I'm guessing you're supposed to find the solution yourself, not already made.

What about (Python3.x) :

line = input().split('+')

try:

numbers = [int(n) for n in line]

except ValueError:

print("Invalid line format.")

exit(1)

else:

result = sum(numbers)

print(result)

What about (Python3.x) :

line = input().split('+')

try:

numbers = [int(n) for n in line]

except ValueError:

print("Invalid line format.")

exit(1)

else:

result = sum(numbers)

print(result)

...