0
import json

def get_stored_username():
    """Get stored username if available."""
    filename = 'username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username

def get_new_username():
    """Prompt for a new username."""
    username = input("What is your name? ")
    filename = 'username.json'
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
    return username

def greet_user():
    """Greet the user by name."""
    username = get_stored_username()
    if username:
        correct = input("Are you " + username + "? (y/n) ")
        if correct == 'y':
            print("Welcome back, " + username + "!")
            return

    # We got a username, but it's not correct.
    # Let's prompt for a new username.
    username = get_new_username()
    print("We'll remember you when you come back, " + username + "!")


greet_user()

The function greet_user() in the code above should be rewritten according to the text below:

The only thing left to address is the nested if statements. This can be cleaned up by moving the code that checks whether the username is correct to a separate function. If you’re enjoying this exercise, you might try making a new function called check_username() and see if you can remove the nested if statement from greet_user().

This is my attempt in try to solve this problem:

def greet_user():
    """Greet the user by name."""
    username = get_stored_username()
    if username:
        check_username()
    username = get_new_username()
    print("We'll remember you when you come back, " + username + "!")

def check_username():
    correct = input("Are you " + username + "? (y/n) ")
    if correct == 'y':
        print("Welcome back, " + username + "!")
    username = get_new_username()
    print("We'll remember you when you come back, " + username + "!")

This is the output from the IDLE:

Traceback (most recent call last):
  File "C:\Users\Documents\python_work\files and exceptions\JSON\remember_me.py", line 41, in <module>
    greet_user()
  File "C:\Users\Documents\python_work\files and exceptions\JSON\remember_me.py", line 29, in greet_user
    check_username()
  File "C:\Users\Documents\python_work\files and exceptions\JSON\remember_me.py", line 34, in check_username
    correct = input("Are you " + username + "? (y/n) ")
UnboundLocalError: local variable 'username' referenced before assignment

This is how the program is supposed to work like:

What is your name? eric
We'll remember you when you come back, eric!

Are you eric? (y/n) y
Welcome back, eric!

Are you eric? (y/n) n
What is your name? ever
We'll remember you when you come back, ever!

Are you ever? (y/n) y
Welcome back, ever!
Nacka
  • 5
  • 1
  • 6
  • I did try to do it myself. – Nacka Mar 01 '19 at 16:42
  • My attempt is posted right now. – Nacka Mar 01 '19 at 16:45
  • 1
    What is the expected output and what is the current output? – jkhadka Mar 01 '19 at 16:50
  • 1
    Since your problem does not hinge on reading the file, simply hard-code that input. Unless your question directly involves reading input, there shouldn't be any hand-provided input: don't make us do superfluous typing. – Prune Mar 01 '19 at 16:57
  • `UnboundLocalError: local variable 'username' referenced before assignment` - read about [scoping-rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – Patrick Artner Mar 01 '19 at 17:01
  • I am a beginner so I don't know how am I supposed to 'hard code' input. And there must be hand-provided input since the work should be done with user-generated data. – Nacka Mar 01 '19 at 17:08

1 Answers1

2

You have no variable username in that scope. You have a local variable by that name inside great_user, but you haven't passed it to check_username. SImply add that parameter / argument.

def greet_user():
    """Greet the user by name."""
    username = get_stored_username()
    if username:
        check_username(username)
    username = get_new_username()
    print("We'll remember you when you come back, " + username + "!")

def check_username(username):
    correct = input("Are you " + username + "? (y/n) ")
    if correct == 'y':
        print("Welcome back, " + username + "!")
    username = get_new_username()
    print("We'll remember you when you come back, " + username + "!")

Also, your flow control is uncomfortable. check_username should do only that much, return its status, and let the registration of a new user stay with functions already written for that purpose.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • With your code I get this output: – Nacka Mar 01 '19 at 17:16
  • `Traceback (most recent call last): File "C:\Users\luka.ivankovic\Documents\python_work\files and exceptions\JSON\remember_me.py", line 41, in greet_user('luka') TypeError: greet_user() takes 0 positional arguments but 1 was given` – Nacka Mar 01 '19 at 17:17
  • I understand. Thank you for your time. – Nacka Mar 01 '19 at 17:18