Python 程序制作一个简单的计算器

5 Sept 2024 | 2 分钟阅读

在Python中,我们可以创建一个简单的计算器来执行不同的算术运算,例如加法、减法、乘法和除法。

方法

  • 我们可以从a、b、c和d的选项中选择所需的运算。
  • 我们可以取两个数字,并使用if…elif…else分支来执行特定操作。
  • 我们将使用add()、subtract()、multiply()和divide()函数来评估计算器中的相应操作。

示例

代码:简单计算器

输出

案例 - (1)

Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d):  d
Please enter the first number:  1
Please enter the second number:  2
1 / 2 = 0.5

案例 - (2)

Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): b
Please enter the first number:  12
Please enter the second number:  11
12 - 11 = 1

下一个主题Python显示日历