Home > Mobile >  Simple Calculator python Sololearn
Simple Calculator python Sololearn

Time:01-05

I am to write a program by taking two integers as input and output their sum on Sololearn using python But I don’t seem to get what they want me to do

CodePudding user response:

I am to write a program by taking two integers as input and output their sum on Sololearn using python But I don’t seem to get what they want me to do

This is what the lesson is asking you to do.

  • Step 1: Take two integers as input
  • Step 2: Output their sum (addition)

Step 1 Using the input() function wrapped in the int() type allows a user to input a number from the command line.

num1 = int(input('Enter the 1st number: '))
num2 = int(input('Enter the 2nd number: '))

Step 2 Add the two entered numbers using the ' ' operand

sum_of_two_numbers = num1   num2
print('The sum of two numbers is', sum_of_two_numbers)

I suggest wrapping the code in a try-and-catch block.

import traceback, os 
try:
    num1 = int(input('Enter the 1st number: '))
    num2 = int(input('Enter the 2nd number: '))

    sum_of_two_numbers = num1   num2
    print('The sum of two numbers is', sum_of_two_numbers)

except Exception as ex:
          
    template = "An exception of type {0} occurred. Arguments:{1!r}"
    message = template.format(type(ex).__name__, ex.args)
    print( message )

    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print(exc_type, fname, exc_tb.tb_lineno)
    print(traceback.format_exc())

CodePudding user response:

num1 = int(input('Enter your first number: '))
num2 = int(input('Enter your second number: '))
print('Your sum is', num1   num2)

I would suggest doing more research on the input function in python. Also, in the future, please provide the code that you have already written so that it is easier to answer your question.

CodePudding user response:

Try with

a = int(input("A: ")) # Convert input string to int
b = int(input("B: "))
print("A   B =", a b)
  •  Tags:  
  • Related