0

im new ish to python and ive got this code and i want it to allow for the word "exit" to be entered at any time when inputting. And once that is done, it closes the script.

Any help would be really appreciated.

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()
1
  • there are no loops here. Commented Mar 9, 2017 at 9:13

2 Answers 2

1

You can just encapsulate input function to exit on the "exit" word :

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()
Sign up to request clarification or add additional context in comments.

Comments

0

you can check every time after input that it is exit or not. if it is exit then terminate the program. as your code you can put the code given bellow after every time of taking input

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.