2017-07-31 16 views
0

初めての訪問時にユーザー名とパスワードを入力して外部ファイルに保存できるプログラムを作成しようとしています。これはowで動作していますが、既にアカウントを持っているので、外部ファイルにテキストが見つかった場合は、プログラムを直接スキップしてログインしてもらいたいです。ユーザーがログインすると、プログラムはそれを外部ファイルのデータと比較し、正しいユーザー名とパスワードが入力されるまで続けます。アカウントにログインして登録する

私は、コードを入力しようとしたが、これらのエラーが出てくる:

Traceback (most recent call last): 
line 27, in <module> 
    login() 
line 16, in login 
    check() 
line 19, in check 
    if username == open("username").read() and passsword == open("password").read(): 
NameError: name 'username' is not defined 
def make_account(): 

    filename = ("username"); 
    with open (filename, "w") as f: 
     f.write (input("Enter a username: ")); 

    filename = ("password"); 
    with open (filename, "w") as f: 
     f.write (input("Enter a password: ")); 


def login(): 
    username = input("Enter your username: ") 
    password = input("Enter your password: ") 
    check() 

def check(): 
    if username == open("username").read() and passsword == open("password").read(): 
     print("Successful login") 
    else: 
     print('Incorrect') 


import os.path 
if os.path.exists("username"): 
    login() 
else: 
    make_account() 
+0

'username'は' check() 'のスコープに定義されていません。 – Arount

答えて

2

usernamepasswordcheck機能ではスコープではありません。

def login(): 
    username = input("Enter your username: ") 
    password = input("Enter your password: ") 
    check(username, password) # note: passing in here 

def check(username, password): # accept the parameters here 
    if username == open("username").read() and password == open("password").read(): 
     print("Successful login") 
    else: 
     print('Incorrect') 
関連する問題