帮我用python 写一个计算器的程序
时间: 2023-08-15 10:03:59 浏览: 101
好的,下面是一个简单的 Python 计算器程序:
```python
while True:
try:
num1 = float(input("请输入第一个数:"))
num2 = float(input("请输入第二个数:"))
operator = input("请输入运算符:")
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
print("运算符输入错误,请重新输入")
continue
print("结果为:", result)
except:
print("输入有误,请重新输入")
```
这个程序可以进行简单的加减乘除运算,你可以按照提示输入两个数和运算符,程序会自动计算并输出结果。如果输入有误,程序会提示并要求重新输入。
相关问题
帮我用python写一个计算器
好的,以下是一个简单的计算器的示例代码:
```
while True:
try:
equation = input('请输入公式:')
if equation == 'exit':
break
result = eval(equation)
print(result)
except:
print("请输入正确的公式!")
```
您可以运行这段代码并输入类似于"2+3"的公式进行计算。记得输入 "exit" 可以退出程序。
使用python写一个计算器程序
下面是一个简单的 Python 计算器程序代码:
```
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
print("选择运算:")
print("1.加法")
print("2.减法")
print("3.乘法")
print("4.除法")
choice = input("输入你的选择(1/2/3/4):")
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("非法输入")
```
运行这个程序,用户会被提示输入一个整数来选择所要执行的运算,然后程序会询问输入两个数字。接着,根据用户的选择,程序会调用相应的函数并输出结果。
阅读全文
相关推荐
![](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)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)