简体   繁体   English

简单计算器 python Sololearn

[英]Simple Calculator python Sololearn

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我将通过将两个整数作为输入和 output 他们在 Sololearn 上的总和使用 python 来编写程序但我似乎没有得到他们想要我做的事情

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我将通过将两个整数作为输入output 他们在 Sololearn 上的总和使用 python 来编写程序但我似乎没有得到他们想要我做的事情

This is what the lesson is asking you to do.这就是课程要求你做的事情。

  • Step 1: Take two integers as input第一步:取两个整数作为输入
  • Step 2: Output their sum (addition)第2步:Output他们的总和(加法)

Step 1 Using the input() function wrapped in the int() type allows a user to input a number from the command line.步骤 1 使用包装在int()类型中的input() function 允许用户从命令行输入数字。

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

Step 2 Add the two entered numbers using the '+' operand步骤 2 使用“+”操作数添加两个输入的数字

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.我建议将代码包装在try-and-catch块中。

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())
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.我建议对 python 中的input function 进行更多研究。 Also, in the future, please provide the code that you have already written so that it is easier to answer your question.另外,将来请提供您已经编写的代码,以便更容易回答您的问题。

Try with尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM