nit*_*irl 0 python loops python-3.x
我用 python 3.4 编写了一个 BMI 计算器,最后我想询问用户是否愿意再次使用该计算器,如果是,请返回到代码的开头。到目前为止我已经得到了这个。非常欢迎任何帮助:-)
#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" :
Run Code Online (Sandbox Code Playgroud)
....
将整个代码包装成一个循环:
while True:
Run Code Online (Sandbox Code Playgroud)
每隔一行缩进 4 个字符。
每当你想“从头开始”时,使用语句
continue
Run Code Online (Sandbox Code Playgroud)
每当您想终止循环并继续执行时,请使用
break
Run Code Online (Sandbox Code Playgroud)
如果您想终止整个程序,请import sys
在代码开头(在-- 重复!-之前没有用),并且每当您想终止程序时,请使用while True:
import
sys.exit()
Run Code Online (Sandbox Code Playgroud)