Python计算器简单程序

Python计算器 (Python Calculator)

In this quick post, we will see how we can create a very simple python calculator program. We will take input from the user about the operation he wants to perform and show the result on its basis. Let’s get started with our python calculator code right away.

python calculator

在这篇快速文章中,我们将看到如何创建一个非常简单的python计算器程序。 我们将从用户输入有关他要执行的操作的信息,并根据其显示结果。 让我们立即开始使用我们的python计算器代码。

模块化代码 (Modular Code)

To keep things modular, we will define functions to perform various operations. Here are the operations our calculator will support for numbers:

为了使事物保持模块化,我们将定义函数来执行各种操作。 这是我们的计算器支持的数字运算

  • Addition

    加成
  • Subtraction

    减法
  • Multiplication

    乘法
  • Division

  • Raising a power to number

    提高数字能力

加成 (Addition)

Let’s do our first operation of Addition here:

让我们在这里进行加法的第一个操作:

def addition(x, y):
   return x + y;

We used a simple function so that our code remains modular.

我们使用了一个简单的函数,以便我们的代码保持模块化。

减法 (Subtraction)

Second operation involves subtracting two numbers:

第二个操作涉及两个数字相减:

def subtraction(x, y):
   return x - y;

乘法 (Multiplication)

Third operation involves multiplying two numbers:

第三个运算涉及两个数字相乘:

def multiplication(x, y):
   return x * y;

(Division)

Fourth operation involves dividing two numbers:

第四个运算涉及两个数的除法:

def division(x, y):
   return x / y;

提高数字能力 (Raising a power to number)

Our final operation involves raising a number by a power. Note that to do this, we can use mathematical operator **:

我们的最终操作涉及通过幂提高数字。 注意,要做到这一点,我们可以使用数学运算符**

def raisePower(x, y):
   return x ** y;

接受用户输入 (Taking user input)

Time to present the user with available choices and taking an input from him:

是时候向用户展示可用的选项并从他那里获取输入了:

print("Operation to perform:");
print("1. Addition");
print("2. Subtraction");
print("3. Multiplication");
print("4. Division");
print("5. Raising a power to number");

choice = input("Enter choice: ");

num1 = int(input("Enter first number: "));
num2 = int(input("Enter second number: "));

决定经营 (Deciding operation)

Finally, we can decide which function to call when user has provided an input:

最后,我们可以决定在用户​​提供输入后要调用哪个函数:

if choice == '1':
   print(num1, "+" ,num2, "=", addition(num1, num2));

elif choice == '2':
   print(num1, "-", num2, "=", subtraction(num1, num2));

elif choice == '3':
   print(num1, "*", num2, "=", multiplication(num1, num2));

elif choice == '4':
   print(num1, "/", num2, "=", division(num1, num2));

elif choice == '5':
   print(num1, "**", num2, "=", raisePower(num1, num2));

else:
   print("Please select a valid input.");

When we run this program, we will be able to perform mathematical operations:

python calculator simple program

运行此程序时,我们将能够执行数学运算:

结论 (Conclusion)

In this quick post, we defined a very simple Python calculator and kept the code modular as well so that we can reuse the functions accordingly.

在这篇快速文章中,我们定义了一个非常简单的Python计算器,并保持了代码模块化,以便我们可以相应地重用这些函数。

翻译自: https://www.journaldev.com/18798/python-calculator-simple-program

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值