import json
filename = "user_names.json"
def greet_user():
print("Welcome to My program that asks and then remembers your name.")
try:
name = stored_user()
except FileNotFoundError:
new_user()
query = input(f"We have detected a possible previous user. Is your name {name}? Enter Y or N to proceed.\n")
answer = query.upper()
if answer != "Y" and "N":
error()
elif answer == "N":
new_user()
elif answer == "Y":
name = stored_user()
print(f"Welcome back, {name}.")
goodbye()
def stored_user():
with open(filename) as f:
name = json.load(f)
return name
def new_user():
name = input("Please enter your name. I will remember it next time you come back on the site. \n")
with open(filename, "w") as f:
json.dump(name, f)
goodbye()
def goodbye():
print("Thank you for using my program.")
def error():
print("Only 'Y' and 'N' responses are considered valid.\nPlease try again")
greet_user()
greet_user()
When I run the program a first time everything goes correctly. Asks for the users name then stores it in a newly created json file. However when you run the file a second time and choose to not be the previous user by pressing N in the if loop it does not go into the new_user() function but instead the error function. Oddly when I press Y it correctly will go into the welcome back prompt and goodbye function. Any reasons why?