如何循环回到程序的开头-Python [英] How to loop back to the beginning of a programme - Python
问题描述
我在 python 3.4 中编写了一个 BMI 计算器,最后我想问一下用户是否想再次使用计算器,如果是,请返回代码的开头.到目前为止,我已经知道了.任何帮助都非常欢迎:-)
I've written a BMI calculator in python 3.4 and at the end I'd like to ask whether the user would like to use the calculator again and if yes go back to the beginning of the code. I've got this so far. Any help is very welcome :-)
#Asks if the user would like to use the calculator again
again =input("Thank you again for using this calculator, would you like to try again? Please type y for yes or n for no-")
while(again != "n") and (again != "y"):
again =input("Please type a valid response. Would you like to try again? Please type y for yes or n for no-")
if again == "n" :
print("Thank you, bye!")
elif again == "y" :
....
推荐答案
将整个代码包装成一个循环:
Wrap the whole code into a loop:
while True:
每隔一行缩进4个字符.
indenting every other line by 4 characters.
只要您想从头开始",请使用语句
Whenever you want to "restart from the beginning", use statement
continue
只要您想终止循环并在循环后继续进行,请使用
Whenever you want to terminate the loop and proceed after it, use
break
如果您要终止整个程序,请在代码开头 import sys
(之前 import
!-),并在每次要终止程序时使用
If you want to terminate the whole program, import sys
at the start of your code (before the while True:
-- no use repeating the import
!-) and whenever you want to terminate the program, use
sys.exit()
这篇关于如何循环回到程序的开头-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!