dimanche 13 janvier 2019

Running a while loop if keys aren't in a dictionary

I'm currently trying to build a very simple program where the user is asked to choose a path and based on the path chosen I want to update a dictionary. Most of my code seems to be running fine but when the user chooses "path_2b" first the while loop breaks. I'm just starting to learn the ropes of Python and programming in general so any help and tips are appreciated!

user_save = {}

def start_button():
    def path_2a():
        if "path_1" in user_save:
            print("You've already taken this path.")
        else:
            user_save["path_1"] = "completed"
            print("Congrats on finishing this path!")
    def path_2b():
        if "path_2" in user_save:
            print("You've alredy taken this path.")
        else:
            user_save["path_2"] = "Completed"
            print("Congrats on finishing this path!")

    chosen_path = input("Would you like to choose path 2A or 2B?: ").lower()
    if chosen_path == "2a":
        path_2a()
    elif chosen_path == "2b":
        path_2b()
    else:
        print("Sorry that isn't a valid path. Please try again.")

while ("path_1" and "path_2") not in user_save:
    start_button()
if "path_1" and "path_2" in user_save:
    print("Congrats on finishing the game!")


I would like the loop to keep running until the user has chosen both paths 1 and 2. Once both keys are in the dictionary I want to print a congrats message and break the loop. Like I said before most of the code runs fine. If the user chooses path_2a first and then 2b the loops does exactly what I want it to even if they choose a path that isn't there. It's only when the user chooses path_2b first. Thanks for the help!

Aucun commentaire:

Enregistrer un commentaire