2017-03-09 8 views
0

im ish pythonとiveがこのコードを持っていて、入力時に「exit」という単語をいつでも入力できるようにしたい。それが完了すると、スクリプトを閉じます。ループから抜け出す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() 
関連する問題