mercredi 19 août 2020

Program refuses to run 'if' statement despite having a valid value as an input

I'm very new to computer programming and am currently writing a program in PyCharm Community that, when given the name of a student at my school, will print out directions to said student's house from the school.

Everything has been going very well, and I got the base of it working last night. Today I opened my computer and for some reason my program refuses to run my 'if'/'elif' statements and will only run the else statement even when it's given a value that satisfies the 'if'/'elif' statement.

I have tried rewriting the program, restarting PyCharm multiple times, making sure I'm consistent with spaces and tabs, and made sure that my variables can all communicate with each other. I have dug for awhile on here and other websites and I just cannot see a reason as to why my code was working yesterday but now refuses to run anything but the else statement.

Here is my code, it will ask the user "Where would you like to go?" and then will receive an input of " house". Once it receives this it will print out their directions. Instead of this, it runs the 'else' statement every single time.

# Storing the names and directions of users:
David = "Directions to David's home from T... \n East on X, \n South on Y.," \
            " \n West on Z., \n South on A., \n first white house on the right."

Caroline = "Directions to Caroline's home from T... \n East on x, \n South on y.," \
        " \n East on z., \n South on p., \n East on q," \
        " \n West on t., \n last brick house in the cul-de-sac."

William = "Directions to Will's home from T... \n East on x, \n South on y.," \
        " \n West on z., \n South on Fa., \n West on b., \n first house on the right."

Bannon = "<Insert directions to Bannon's house>"

# User gives a specific name and then receives a location:
while True:
    destination = input("Where would you like to go? ")

    if destination.casefold() == 'Davids house':
        print(David)
        continue

    elif destination.casefold() == 'Carolines house':
        print(Caroline)
        continue

    elif destination.casefold() == 'Wills house':
        print(William)
        continue

    elif destination.casefold() == 'Bannons house':
        print(Bannon)
        continue

    # If an invalid location is given, this code will run:
    else:
        print("Sorry, that location wasn't found! Please try again.")
        continue

Aucun commentaire:

Enregistrer un commentaire