请用python写一个计算器
时间: 2023-03-14 11:21:58 浏览: 86
好的,以下是一个简单的 Python 计算器代码:
```
def calculator():
num1 = float(input("请输入第一个数字: "))
operator = input("请输入运算符(+、-、*、/): ")
num2 = float(input("请输入第二个数字: "))
if operator == '+':
result = num1 + num2
print("结果为:", result)
elif operator == '-':
result = num1 - num2
print("结果为:", result)
elif operator == '*':
result = num1 * num2
print("结果为:", result)
elif operator == '/':
result = num1 / num2
print("结果为:", result)
else:
print("运算符无效")
calculator()
```
运行这段代码后,你会得到一个简单的交互式计算器,它支持加、减、乘、除四种运算。
相关问题
请使用python写一个计算器
好的,以下是一个简单的计算器的 Python 代码:
```
def calculator():
operation = input("请选择要进行的操作:+、-、*、/:")
num1 = float(input("请输入第一个数字:"))
num2 = float(input("请输入第二个数字:"))
if operation == "+":
print(num1 + num2)
elif operation == "-":
print(num1 - num2)
elif operation == "*":
print(num1 * num2)
elif operation == "/":
print(num1 / num2)
else:
print("输入有误,请重新输入!")
calculator()
calculator()
```
希望这个代码能够帮助到你!
请帮我用python写一个计算器
当然可以。以下是一个简单的 Python 计算器代码示例:
```
def calculator():
operation = input("请输入运算符(+、-、*、/):")
num1 = float(input("请输入第一个数字:"))
num2 = float(input("请输入第二个数字:"))
if operation == "+":
print(num1 + num2)
elif operation == "-":
print(num1 - num2)
elif operation == "*":
print(num1 * num2)
elif operation == "/":
print(num1 / num2)
else:
print("无效的运算符")
calculator()
```
这是一个简单的计算器,它允许用户输入运算符和两个数字,并执行对应的算术运算。 您可以添加其他功能或修改现有功能,以满足您的需求.