2012-03-11 6 views
0

python 3.2では、実行中の関数の残りの部分を停止する方法はありますか?Pythonでは、実行中の関数でコードをどのように停止しますか?

基本的には私のコースワークのコンセプトとしてログインシステムを作成していますが、私はどこにでもこの回答を見つけることができませんでした。

マイコードはここなど2つのファイル、ログファイルと入出力を処理ロガー、およびそのようなデータベース接続などのメインクラス、ログインコード自体、

に分割されるコードでありますユーザーの入力を処理する、私は3番目と4番目の行に興味がある、 'quit'を 'QUIT0x0'に変換して、事故によって呼び出される終了コードの可能性を最小限に抑える。

def getInput(input_string, type): 
    result = input(input_string) 
    if result.lower == 'quit': 
      result = 'QUIT0x0' 
    #log the input string and result 
    if type == 1: 
      with open(logFile, 'a') as log_file: 
        log_file.write('[Input] %s \n[Result] %s\n' %(input_string, result)) 
        return result 
    #no logging 
    elif type == 2: 
      return result 
    #undefined type, returns 'Undefined input type' for substring searches, and makes a log entry 
    else: 
      result = '[Undefined input type] %s' %(input_string) 
      output(result, 4) 
      return result 

これは、ユーザデータベースからユーザレコードを削除処理するコード、私は4番目と5番目のラインの仕事を作り、実行から機能の残りの部分を停止する方法に興味がある:

事前に
def deleteUser(self): 
self.__user = getInput('Enter the username you want to delete records for: ', 1) 
if self.__user == 'QUIT0x0': 
    #Quit code goes here 
else: 
    self.__userList = [] 
    self.__curs.execute('SELECT id FROM users WHERE username="%s"' %(self.__user)) 

おかげで、 トム

+0

'result.lower'をすべきbe 'result.lower() ' –

+0

なぜ' quit() 'コードが偶然に呼び出されるのですか? –

答えて

6

"機能を終了しますが、" returnと呼ばれている:

def deleteUser(self): 
    self.__user = getInput('Enter the username you want to delete records for: ', 1) 
    if self.__user == 'QUIT0x0': 
    return 
    else: 
    # ... 

しかし既にif/elseを使用しているので、elseブランチは実行しないでください。したがって、復帰は不要です。あなただけのようにもそこにpassを置くことができます:

def deleteUser(self): 
    self.__user = getInput('Enter the username you want to delete records for: ', 1) 
    if self.__user == 'QUIT0x0': 
    pass 
    else: 
    # ... 

、あるいは次のようにします。

def deleteUser(self): 
    self.__user = getInput('Enter the username you want to delete records for: ', 1) 
    if self.__user != 'QUIT0x0': 
    # ... 

、あるいは早期復帰を使用します。

def deleteUser(self): 
    self.__user = getInput('Enter the username you want to delete records for: ', 1) 
    if self.__user == 'QUIT0x0': 
    return 
    # ... 
+0

今私は、愚かな感じ、笑ありがとう:) – Billie

+1

@ビリー: "私はそれを理解していない"または "私は自分でそれを考え出すべきだった"のような愚か者? ;)また、役に立つと思われる場合は、左側のチェックボタンを使用してこれを回答として受け入れることを検討してください。 –

+0

「私は自分でそれを分かったはずだった」と私は思いますが、それはさらに6分間私を私にさせません – Billie

関連する問題