2017-03-09 157 views
0

即时通讯新的ish到python和ive得到了这段代码,我希望它允许在输入时随时输入“退出”一词。一旦完成,它会关闭脚本。从循环python退出3

任何帮助将非常感激。

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() 
+0

这里没有循环。 –

回答

1

你可以只封装输入功能,退出的“退出”二字:

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() 
0

你可以输入,这是退出或不经过检查每一个时间。如果是退出则终止程序。作为您的代码,您可以在每次输入数据后将代码放入波纹管中。

if input(inputs) == "exit": 
     sys.exit()