1

I'm very, very new to computer programming. I started writing a very simple script that gives you directions to someone's house when you input their name:

# Users and Directions:
davids_house = "Directions to David's home from X... \n East on Y, \n South on Z," \
               " \n West on A, \n South on B, \n first C on the right."
bannons_house = "bannons directions here"

# Asking the user where he/she would like to go:
destination = input("Where would you like to go? ")

# Reads out a set of directions depending on what name was picked:
if destination in ['Davids_House', 'davids_house', 'Davids_house', 'davids_House']:
    print(davids_house)

if destination in ['Bannons_House', 'bannons_house', 'Bannons_house', 'bannons_House']:
    print(bannons_house)

else:
    print("Sorry, that's not an option.")

Whenever I run this program and reach the point where it gives directions, I'll give it a valid input of say 'davids_house'. It will print out the directions, but it will ALSO run the 'else' statement. When I type in an invalid value, it does what I want it to do and ONLY displays "Sorry, that's not an option."

I was under the impression that the 'else' will not run unless the 'if' statement isn't satisfied with a valid value.

I haven't been able to find anything like this, surprisingly, on other forums. I've found questions/answers similar to what I'm looking for, but nothing has actually fixed my issue in this specific case. What am I doing wrong?

2
  • 2
    I didn't read the whole question, but maybe try to use elif instead of another if Commented Aug 18, 2020 at 22:51
  • 1
    You've posted a good question. Properly phrased, clear problem statement, etc. Nice work! Commented Aug 18, 2020 at 22:56

3 Answers 3

5

That's because the first two if statement in your program are separated and not related, so the second if still runs even if the first one was True. You should use if..elif..else:

if destination in ['Davids_House', 'davids_house', 'Davids_house', 'davids_House']:
    print(davids_house)

elif destination in ['Bannons_House', 'bannons_house', 'Bannons_house', 'bannons_House']:
    print(bannons_house)

else:
    print("Sorry, that's not an option.")
Sign up to request clarification or add additional context in comments.

Comments

3

You have an if, and a paired if/else that is unrelated to it; sequential ifs aren't innately related to one another, and an else is only tied to one preceding if (possibly with multiple elif blocks between them). If you want the else to only run if all the ifs fail, you need to tie them together, by making the second if an elif tied to the first if. That will get you mutually exclusive behavior; one and only one block will execute:

if destination in ['Davids_House', 'davids_house', 'Davids_house', 'davids_House']:
    print(davids_house)

# Next line changed to elif from if
elif destination in ['Bannons_House', 'bannons_house', 'Bannons_house', 'bannons_House']:
    print(bannons_house)

else:
    print("Sorry, that's not an option.")

1 Comment

Thank you! Exactly what I needed and I appreciate your detailed explanation.
1

It prints the directions to David's house but the else only applies to Bannon's house. To make all of them into a single statement, use elif:

if destination in ['Davids_House', 'davids_house', 'Davids_house', 'davids_House']:
    print(davids_house)
elif destination in ['Bannons_House', 'bannons_house', 'Bannons_house', 'bannons_House']:
    print(bannons_house)
else:
    print("Sorry, that's not an option.")

By the way, if you want to include many different forms of capitalization, you may want to pre-process the string before you compare it. A simple example:

if destination.casefold() == 'davids_house':

2 Comments

Thanks for the response, and I also implemented the casefold string method. That was VERY useful!
@spirebyte. You can always change your selection ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.