2017-11-25 1 views
-1
if login == "y": 
ausername = input("Please enter username") 
apassword = input("please enter your password") 
file = open("up.txt", "r") 
for line in file.readlines(): 
    if re.search("ausername"+"apassword") 

ユーザーがシステムにログインしようとしたときに、ユーザー名とパスワードがファイルに保存されていることを検証したい場合は、ユーザーに戻り、ログインの詳細を再入力します。再試行する。テキストファイルの入力をどのように一致させ、見つからなければループするか?

+0

're'モジュールと' for'複合文についてPythonのドキュメントを読んで、あなたが何かを理解していないのかどうかを尋ねます。 。 –

答えて

0

あなたのログイン要求を別の機能にまとめたいと思うかもしれません。誤った入力による繰り返し呼び出しを含めて、ユーザーにログインの詳細を求めるプロンプトを表示する場合は、いつでもその関数を呼び出すことができます。ラフ例:約

def SomeMainFunction(...): 
    # Everything else you're doing, then login prompt: 
    if login == 'y': 
     success = False 

     while not success: 
      success = LoginPrompt() 
      # While login is unsuccessful the loop keeps prompting again 
      # You might want to add other escapes from this loop. 

def LoginPrompt(): 
    ausername = input("Please enter username") 
    apassword = input("please enter your password") 
    with open("up.txt", "r") as file: 
     for line in file.readlines(): 
      if re.search("ausername"+"apassword"): 
       # return True if login was successful 
       return True 
      else: 
       return False 

「オープンで...」:それは開いているファイル=のように動作しますが、file.closeが暗示されるという利点を有しています。したがって、LoginPromptから戻る前に "file.closed"(スニペットがないファイル)を実行する必要はありません。

私は実際にreに精通していないので、あなたのコードはユーザー名を見つけるために働くと上記で仮定しました。

with open('up.txt', 'r') as file: 
    for line in file.readlines: 
     if ausername and apassword in line: 
      return True 
     ... 
関連する問題