帮我写一段代码。要求实现对四个按键的定义,并分别实现加减乘除功能
时间: 2024-05-09 09:16:54 浏览: 107
关于四则运算加减乘除的代码
5星 · 资源好评率100%
好的,以下是你需要的代码:
```python
# 定义四个按键
key1 = "+"
key2 = "-"
key3 = "*"
key4 = "/"
# 实现加减乘除功能
def calculate(num1, num2, operator):
if operator == key1: # 加法
result = num1 + num2
elif operator == key2: # 减法
result = num1 - num2
elif operator == key3: # 乘法
result = num1 * num2
elif operator == key4: # 除法
result = num1 / num2
else:
print("输入的运算符不正确")
return None
return result
# 示例
num1 = 10
num2 = 5
operator = key1
result = calculate(num1, num2, operator)
print(f"{num1} {operator} {num2} = {result}") # 输出 10 + 5 = 15
```
希望这段代码能够帮助到你。
阅读全文