0
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?

Wade
  • 17
  • 6
  • Does this answer your question? [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value) – Luke B Jan 03 '23 at 01:31
  • Basically you need to do: `if answer != "Y" and answer != "N":`, or : `if answer not in {"Y","N"}:` – Luke B Jan 03 '23 at 01:32
  • Better check for `Y` and `N` in `if`/`elif`, then call the error function in `else`. – Selcuk Jan 03 '23 at 01:35
  • In your specific case you can simply use the `else` clause: `if answer == "N": ... elif answer == "Y" ... else: error()` – blhsing Jan 03 '23 at 01:35

0 Answers0