因此,在测试我的新课程时,我使用了一个while循环。但是当我尝试使用SystemExit命令时;它一直在问我是否想要循环启动程序。有什么方法我仍然可以使用while循环,而不是让最终过程重复? 这是代码:
def main():
print("This program will convert KG's to LBS and vice versa.")
name = input("Please print your name: ")
conversion_input = input("Do you want to convert into KG's or LBS's?")
if conversion_input == 'KG':
pounds = int(input("What weight of lbs do you want to convert? "))
print(pounds, " converted to lbs is: ", pounds * 0.45, "lbs")
if conversion_input == 'LBS':
kilo = int(input("What weight in kg's do you want to convert? "))
print(kilo, " converted to kg is: ", kilo * 2.2, "kgs.")
main()
while True:
n = input('Do you want to try again?')
if n == 'yes':
main()
if n == 'no':
SystemExit
while False:
SystemExit
答案 0 :(得分:0)
正如nneonneo在评论中提到的,SystemExit
不是"命令"。它是调用sys.exit()
时引发的异常的类型,这是你应该在这里做的,例如。
if n == 'no':
sys.exit()
(如果您还没有,则需要在文件开头import sys
使用此功能。)
我也不确定你最后在while False:
循环中尝试做什么。这样的循环永远不会运行。