Python程序将两个数字相加
给定两个数字 num1 和 num2。任务是编写一个Python程序来查找这两个数字的相加。
例子:
Input: num1 = 5, num2 = 3
Output: 8
Input: num1 = 13, num2 = 6
Output: 19
在下面的添加两个数字的程序中,首先要求用户输入两个数字,然后使用 input()函数扫描输入并将其存储在变量 number1 和 number2 中。然后,使用算术运算运算符+ 将变量 number1 和 number2 相加,并将结果存储在变量 sum 中。
下面是两个数字相加的Python程序:
示例 1:
# Python3 program to add two numbers
num1 = 15
num2 = 12
# Adding two nos
sum = num1 + num2
# printing values
print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))
输出:
Sum of 15 and 12 is 27
示例 2:添加用户输入提供的两个数字
# Python3 program to add two numbers
number1 = input("First number: ")
number2 = input("\nSecond number: ")
# Adding two numbers
# User might also enter float numbers
sum = float(number1) + float(number2)
# Display the sum
# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1, number2, sum))
输出:
First number: 13.5 Second number: 1.54
The sum of 13.5 and 1.54 is 15.04