あなたのログイン要求を別の機能にまとめたいと思うかもしれません。誤った入力による繰り返し呼び出しを含めて、ユーザーにログインの詳細を求めるプロンプトを表示する場合は、いつでもその関数を呼び出すことができます。ラフ例:約
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
...
're'モジュールと' for'複合文についてPythonのドキュメントを読んで、あなたが何かを理解していないのかどうかを尋ねます。 。 –