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](https://i-blog.csdnimg.cn/blog_migrate/45b662d911f8ad9b90e756b9d95f0897.png)
在这篇快速文章中,我们将看到如何创建一个非常简单的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](https://cdn.journaldev.com/wp-content/uploads/2018/02/python-calculator.png)
运行此程序时,我们将能够执行数学运算:
结论 (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