python函数实现计算器
时间: 2023-12-08 10:04:29 浏览: 93
以下是一个简单的 Python 函数实现计算器的例子:
```python
def calculator(num1, num2, operator):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
if num2 == 0:
return "Error: Division by zero"
else:
return num1 / num2
else:
return "Error: Invalid operator"
```
这个函数接受三个参数:两个数字和一个运算符。根据运算符的不同,函数执行不同的计算操作,然后返回结果。如果运算符无效或者除数为零,则函数返回一个错误消息。
例如,调用 `calculator(5, 3, '+')` 将返回 `8`,调用 `calculator(10, 2, '/')` 将返回 `5.0`,调用 `calculator(4, 0, '/')` 将返回 `"Error: Division by zero"`。
相关问题
python用函数实现计算器
以下是一个简单的Python计算器函数,可以实现加、减、乘、除四种基本运算:
```
def calculator(operation, num1, num2):
if operation == '+':
return num1 + num2
elif operation == '-':
return num1 - num2
elif operation == '*':
return num1 * num2
elif operation == '/':
if num2 == 0:
return "Error: division by zero"
else:
return num1 / num2
else:
return "Error: invalid operation"
```
这个函数接受三个参数:操作符(加、减、乘、除)、第一个数和第二个数。它会根据操作符执行相应的运算,并返回结果。如果操作符不是加、减、乘、除中的一个,或者除数为零,则会返回错误信息。
例如,我们可以调用这个函数来计算 2+3、5-1、4*6 和 8/2:
```
print(calculator('+', 2, 3)) # 输出 5
print(calculator('-', 5, 1)) # 输出 4
print(calculator('*', 4, 6)) # 输出 24
print(calculator('/', 8, 2)) # 输出 4.0
print(calculator('/', 6, 0)) # 输出 Error: division by zero
print(calculator('%', 6, 3)) # 输出 Error: invalid operation
```
注意,这个函数只能处理两个数之间的运算,如果需要计算更复杂的表达式,可以使用 Python 的 eval() 函数。例如,eval("2 + 3 * 4") 会返回 14。不过需要注意,eval() 函数存在一些安全风险,因为它可以执行任意的 Python 代码,所以在处理用户输入时需要格外小心。
python用函数实现简单计算器
以下是一个简单的 Python 函数,实现基本的加减乘除运算:
```python
def calculator(num1, num2, operator):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
return num1 / num2
else:
return 'Invalid operator'
```
使用示例:
```python
>>> calculator(2, 3, '+')
5
>>> calculator(2, 3, '-')
-1
>>> calculator(2, 3, '*')
6
>>> calculator(2, 3, '/')
0.6666666666666666
>>> calculator(2, 3, '%')
'Invalid operator'
```
以上代码只是一个简单的示例,实际上计算器需要考虑更多的情况,例如输入的数字和操作符的类型、精度等问题。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)