import json def main(): print("You are logged in!") def login(): print("Welcome to Password Generator, please sign in.") # See below. Name this something more descriptive or follow whatever existing convention: ---+ # | # +------------------------------------------------------------+ # | # v with open("info.json", "r") as x_read: reading = x_read.readlines() user = reading[0] pwd = reading[1] # If the contents of info.json is an actual json-encoded object, you can # load it as a native Python dict with: user_info = json.loads(x_read.read()) # Now you can pull the values out of it with: pwd = user_info['secret'] user = user_info['name'] print(pwd) print(user) username = input("Enter username? ") password = input("Enter password? ") if user.strip() == username and pwd.strip() == password: # See below. No need to close the file stream if you open it with "with". # However, check your scope. Here, you're outsite the "with" block, # so x_read is out of scope. x_read.close() main() else: print("Invalid Password! or Username!") exit() # If you want extra credit, rewrite the function that saves the user's password to # info.json, but don't store the password in plain text. Instead, try _hashing_ # the password so that the user's sensitive information (the password) remains save # even if someone ill-meaning gets a hold of info.json. # For double extra credit: rewrite register() such that you can now save the # username and password for more than one user. This could be a good opportunity # to check if the provided username and/or email have already been registered. def register(): registered = input("Would you like to register (yes) or are you already a member (member)? ") # 1. Convert whatever the input is to lowercase to simplify your conditionals registered = registered.lower() if registered == "yes": # 2. A common convention is to name a file reference as either "reader" or "writer" # depending on how you open the file. Here you had named it "x_append" ---+ # | # +---------------------------+ # | # v with open("info.json", "w", encoding="utf8") as writer: name = input("Username? ") writer.write(name + "\n") secret = input("Password? ") writer.write(secret + "\n") email = input("What's your email address? ") writer.write(email + "\n") # 3. Because writer exists within the contenxt of a "with" block, there's no # need to explicitly close() it. The "with" block will close the file # reference for you. writer.close() # 4. Again, by convention, you might want to name this variable "reader" instead # of "x_read" because reader is more descriptive (and a convention). with open("info.json", "r") as reader: read = reader.readlines() print(read[0]) print(read[1]) # 5. Again, don't close a stream managed by "with". # reader.close() login() # Another thing... here you did two things: 1. wrote to a file 2. read from that # same file right after. What's the point of reading from the file right after you # wrote to it? # Also... the name of your file is "info.json". By convention, if the file extension # is .json, you would assume that the contents of the file are json-encoded. However, # here you just write two lines of text without ever json-encoding it. A better, more # conventional approach: # A. Create a dict with empty values for the keys you want to store in your info.json file. user_info = { 'name': None, 'secret': None, 'email': None, } # B. Get the input from the user, storing the input into the dict from the previous step. user_info['name'] = input("Username? ") user_info['secret'] = input("Password? ") user_info['email'] = input("What's your email address? ") # C. Save the dict as a json-encoded object. This way, when you load that object back into # Python, the same structure can be loaded into memory again. That's much easier to # parse than "first line, second line, etc..." with open('info.json', 'w') as writer: writer.write(json.dumps(user_info)) elif registered == "member": login() else: print("No worries, thank you.") exit() register()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question