python - 如何用循环制作计算器,直到我选择中断
问题描述
我正在尝试构建一个带有循环的计算器,直到我选择打破它或结束它。你能建议吗?提前谢谢你,马克斯
new_operation = input("press enter to make a new operation or type the word exit to finish")
num1 = int(input("Enter a number: "))
op = input("Enter the operator: ")
num2 = int(input("Enter a second number: "))
while new_operation != ("no"):
if op == ("+"):
print (num1 + num2)
elif op == ("-"):
print (num1 - num2)
elif op == ("*"):
print (num1 * num2)
elif op == ("/"):\
print (num1 / num2)
else:
print ("Invalid operation")
new_operation = input("make a new operation")
解决方案
您的代码看起来不错,但需要进行一些调整以使其成为“do while”循环类型的实现。
while True:
num1 = int(input("Enter a number: "))
op = input("Enter the operator: ")
num2 = int(input("Enter a second number: "))
if op == ("+"):
print (num1 + num2)
elif op == ("-"):
print (num1 - num2)
elif op == ("*"):
print (num1 * num2)
elif op == ("/"):\
print (num1 / num2)
else:
print ("Invalid operation")
new_operation = input("press enter to make a new operation or type the word exit to finish")
if(new_operation == ("no")):
break
推荐阅读
- r - 按组连接
- c# - 用dependencyinjection替换autofac后根本没有调用asp.net核心控制器构造函数
- javascript - 将特定的 JS 对象值附加到它被点击的 div
- python-3.x - Python3 requests.get 太慢了
- php - 从用户 id 检索用户数据
- laravel - 如何从控制器向laravel中的资源发送附加数据
- android - 如何安装 Splunk Mint Gradle 插件?
- python - 如何确保 pip 从内部 pypi 获取包?
- javascript - 如何以 html 形式获取地理位置并将其发布到 php/sql 数据库中
- android - 存在模拟器时 Flutter 应用程序未运行