实现计算器功能jupyter
时间: 2024-09-11 12:08:36 浏览: 60
在Jupyter Notebook中实现计算器功能,通常会利用Python的交互式特性。你可以创建一个简单的函数,接收用户输入的两个数字和操作符,然后根据操作符执行相应的计算。下面是一个基本的例子:
```python
def calculator():
# 获取用户输入
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 == '/':
if num2 != 0:
result = num1 / num2
else:
print("错误:除数不能为零")
return
else:
print("错误:无效的操作符")
return
print(f"结果是: {result}")
# 调用计算器函数
calculator()
```
在这个例子中,用户可以在Jupyter notebook的环境中直接运行这个`calculator()`函数来体验计算器功能。
阅读全文
相关推荐


















