python - 它继续打印
问题描述
当我运行此代码时,它不断给我答案,这是我的代码
num1 = float(raw_input("enter a number: ")) # type: float
operation = str(raw_input("enter a operation: "))
num2 = float(raw_input("enter a number: ")) # type: float
while True:
if operation == "+":
print num1 + num2
elif operation == "-":
print num1 - num2
elif operation == "*":
print num1 * num2
elif operation == "/":
print (num1 / num2)
else:
print("Error Error")
解决方案
您可能想要的是将输入代码放入 while 循环中:
while True:
num1 = float(raw_input("enter a number: ")) # type: float
operation = str(raw_input("enter a operation: "))
num2 = float(raw_input("enter a number: ")) # type: float
if operation == "+":
print (num1 + num2)
elif operation == "-":
print (num1 - num2)
elif operation == "*":
print (num1 * num2)
elif operation == "/":
print (num1 / num2)
else:
print("Error Error")
推荐阅读
- apache-kafka - Confluent Kafka如何在不订阅的情况下获取特定主题元数据
- firebase - Flutter Firebase 文档是随机排序的
- java - Kivy Android 相机 API 2 - 相机旋转
- angular - 为什么角度应用程序无法识别角度材料样式?
- c++ - 从不同的班级访问班级的麻烦
- c - 传感器的 C 代码仅打印输出一次并退出
- python - 如何使用 Python MAPI 从 Outlook 获取“收件人”姓名和电子邮件地址
- google-cloud-platform - 使用 TaggedOutputs 中断数据流,“无法腌制 WeakDictionary”
- unity3d - NavMeshAgent.SetDestination Y轴问题
- android - SonarLint:删除不必要的布尔文字