Exercise 36 LPTHW

Here is my code for one of the functions I defined. I noticed that whether or not I use the while loop is not making a difference. I think that might be because in each of the else: , I make it jump back to the beginning of the function. How would I make the while loop actually come into play?
(I’ve currently put a # on each of the parts I didn’t use, which didn’t make a difference to when I run my code.


def san_diego():
    print("Good choice! The weather is great here.")
    print("Do you want to go to: Gaslamp or the beach?")
    #want_to = False

    #while True:
    choice = input("Enter your choice: ")

    if "Gaslamp" in choice:
        print("Tough luck. You drink too much and pass out!")
        exit(0)
    elif "beach" in choice:
        print("Great choice.You now have two more options.")
        print("Do you want to build a bornfire or play in the water?")
        #want_to = True

        choose = input("Your choice >>> ")

        if choose == "bornfire":
            print("You enjoy hot cocoa!")
            exit(0)
        elif choose == "water":
            water_play()
        else:
            print("I did not get that. Start over.")
            san_diego()
    else:
        print("Error. Try again.")
        san_diego()

Please give me some suggestions. Thank you!

You forgot the forward slash on the last [code] to close the tag and format the code.

For the while tool to come into play, think about ‘returning false’ somewhere to break the loop.

Eg

choice = input(“> “)
while True: # the loop is started 

    if choice < 5:
        print(“Small numbers”)
    else:
        print(“Game over”)
        return false # breaks the while true statement 
1 Like

Yes, so your logic always calls some other function to exit this one. For the while loop to have any impact you’d have to make the else: not call san_diego(). Then it will go back to the top of the loop instead.

1 Like