gpt4 book ai didi

python - 如何终止Python 中的程序?

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:57 26 4
gpt4 key购买 nike

这是我的项目描述:

编写一个显示以下菜单的程序:

1) Convert Fahrenheit to Celsius
2) Convert Celsius to Fahrenheit

Enter 1, 2, or 0 to exit:

对于选项 1 和 2,程序应提示用户输入温度、执行转换并输出结果。然后程序应该重新显示菜单。当选择选项 0 时,程序应该退出。

这是我到目前为止所拥有的:

a = raw_input("1) Convert Fahrenheit to Celsius \n2) Convert Celsius to Fahrenheit \nEnter 1, 2, or 0 to exit: ")

x = raw_input("Enter degree: ")

y = float((x - 32) / 1.8)

x = raw_input("Enter degree: ")

z = float((x + 32) * 1.8)

if a == 1:
print(y)

if a == 2:
print(z)

如何终止该程序?我还有什么地方搞砸了?

最佳答案

  • 删除第二个 x = raw_input(... 行。您不需要它。
  • 打印之前在if block 内计算y。做用户不想要的数学是没有意义的。
  • z 相同:在需要之前计算它。
  • 将其放在 a = raw_input(... 行之后:

代码:

if a == 0:
import sys
sys.exit(0)

关于python - 如何终止Python 中的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7369527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com