运用python 编写一个计算器,获取键盘输入的两个数,根据运算符号,进行加减乘除
时间: 2024-03-06 17:49:21 浏览: 34
好的,以下是 Python 3 的计算器代码示例:
```python
# 定义计算函数
def calculate(num1, operator, num2):
if operator == "+":
return num1 + num2
elif operator == "-":
return num1 - num2
elif operator == "*":
return num1 * num2
elif operator == "/":
return num1 / num2
else:
return "输入的运算符号不正确!"
# 获取键盘输入
num1 = float(input("请输入第一个数字: "))
operator = input("请输入运算符号(+, -, *, /): ")
num2 = float(input("请输入第二个数字: "))
# 计算结果并输出
result = calculate(num1, operator, num2)
print(f"{num1} {operator} {num2} = {result}")
```
您可以将此代码复制并粘贴到 Python 环境中,或将其保存为 .py 文件并在命令行中运行。运行后,程序会提示您输入两个数字和运算符号,然后计算结果并输出。请注意,此程序仅处理基本的加减乘除运算,不包括其他高级运算。
相关问题
编写一个计算器,获取键盘输入的两个数,根据运算符号,进行加减乘除
好的,我可以为您提供一个简单的计算器程序,以下是Python 3的代码示例:
```python
# 获取键盘输入
num1 = float(input("请输入第一个数字: "))
operator = input("请输入运算符号(+, -, *, /): ")
num2 = float(input("请输入第二个数字: "))
# 计算结果
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
print("输入的运算符号不正确!")
exit()
# 输出结果
print(f"{num1} {operator} {num2} = {result}")
```
您可以将此代码复制并粘贴到 Python 环境中,或将其保存为 .py 文件并在命令行中运行。运行后,程序会提示您输入两个数字和运算符号,然后计算结果并输出。请注意,此程序仅处理基本的加减乘除运算,不包括其他高级运算。
编写一个简单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 == '/':
if num2 == 0:
return "Error: division by zero"
else:
return num1 / num2
else:
return "Error: invalid operator"
```
使用方式:
```python
# 两数相加
result = calculator(10, 5, '+')
print(result) # 输出 15
# 两数相减
result = calculator(10, 5, '-')
print(result) # 输出 5
# 两数相乘
result = calculator(10, 5, '*')
print(result) # 输出 50
# 两数相除
result = calculator(10, 5, '/')
print(result) # 输出 2.0
# 除数为 0,返回错误提示
result = calculator(10, 0, '/')
print(result) # 输出 "Error: division by zero"
# 操作符错误,返回错误提示
result = calculator(10, 5, '%')
print(result) # 输出 "Error: invalid operator"
```
希望能够帮助到您!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)