else:
2 username = get_new_username(path)
print(f"We'll remember you when you come back, {username}!")
greet_user()
Each function in this final version of remember_me.py has a single, clear
purpose. We call greet_user(), and that function prints an appropriate mes-
sage: it either welcomes back an existing user or greets a new user. It does
this by calling get_stored_username() 1, which is responsible only for retriev-
ing a stored username if one exists. Finally, if necessary, greet_user() calls
get_new_username() 2, which is responsible only for getting a new username
and storing it. This compartmentalization of work is an essential part of
writing clear code that will be easy to maintain and extend.
TRY IT YOURSELF
10-11. Favorite Number: Write a program that prompts for the user’s favorite
number. Use [Link]() to store this number in a file. Write a separate pro-
gram that reads in this value and prints the message “I know your favorite
number! It’s _____.”
10-12. Favorite Number Remembered: Combine the two programs you wrote in
Exercise 10-11 into one file. If the number is already stored, report the favorite
number to the user. If not, prompt for the user’s favorite number and store it in a
file. Run the program twice to see that it works.
10-13. User Dictionary: The remember_me.py example only stores one piece of
information, the username. Expand this example by asking for two more pieces
of information about the user, then store all the information you collect in a
dictionary. Write this dictionary to a file using [Link](), and read it back
in using [Link](). Print a summary showing exactly what your program
remembers about the user.
10-14. Verify User: The final listing for remember_me.py assumes either that the
user has already entered their username or that the program is running for the
first time. We should modify it in case the current user is not the person who last
used the program.
Before printing a welcome back message in greet_user(), ask the user if
this is the correct username. If it’s not, call get_new_username() to get the correct
username.
206 Chapter 10