退出循环 python 3
Exit from a loop python 3
我是 python 的新手,我得到了这段代码,我希望它允许在输入时随时输入单词 "exit"。一旦完成,它就会关闭脚本。
任何帮助将不胜感激。
import sys
while True:
def inputs():
first=input("Enter your first name: ")
last=input("Enter your last name: ")
gender=input("Enter your gender: ")
form=input("Enter your form: ")
file = open("signup.txt", "a")
#Records the user's details in the file
file.write("\nFirst name: "+first+", Last name: "+last+", Gender: "+gender+", Form: "+form)
#Closes the file
file.close()
if input(inputs) == "exit":
sys.exit()
inputs()
你可以封装输入函数在"exit"字处退出:
import sys
def exitInput(prompt):
pInput = input(prompt)
return pInput if pInput != "exit" else sys.exit()
def inputs():
first = exitInput("Enter your first name: ")
last = exitInput("Enter your last name: ")
gender = exitInput("Enter your gender: ")
form = exitInput("Enter your form: ")
file = open("signup.txt", "a")
file.write("\nFirst name: "+first+", Last name: "+last+", Gender: "+gender+", Form: "+form)
file.close()
inputs()
每次输入后都可以检查是否退出。如果是 exit 则终止程序。作为您的代码,您可以在每次输入后输入下面给出的代码
if input(inputs) == "exit":
sys.exit()
我是 python 的新手,我得到了这段代码,我希望它允许在输入时随时输入单词 "exit"。一旦完成,它就会关闭脚本。
任何帮助将不胜感激。
import sys
while True:
def inputs():
first=input("Enter your first name: ")
last=input("Enter your last name: ")
gender=input("Enter your gender: ")
form=input("Enter your form: ")
file = open("signup.txt", "a")
#Records the user's details in the file
file.write("\nFirst name: "+first+", Last name: "+last+", Gender: "+gender+", Form: "+form)
#Closes the file
file.close()
if input(inputs) == "exit":
sys.exit()
inputs()
你可以封装输入函数在"exit"字处退出:
import sys
def exitInput(prompt):
pInput = input(prompt)
return pInput if pInput != "exit" else sys.exit()
def inputs():
first = exitInput("Enter your first name: ")
last = exitInput("Enter your last name: ")
gender = exitInput("Enter your gender: ")
form = exitInput("Enter your form: ")
file = open("signup.txt", "a")
file.write("\nFirst name: "+first+", Last name: "+last+", Gender: "+gender+", Form: "+form)
file.close()
inputs()
每次输入后都可以检查是否退出。如果是 exit 则终止程序。作为您的代码,您可以在每次输入后输入下面给出的代码
if input(inputs) == "exit":
sys.exit()