0

My if statement here in line 4 and 6 keep running even if the user inputs the correct word. I'm confused. Any suggestions? Have been trying to get this to work for a day now.

boi = input("Do you want to enter a part of a house or a word, (\"house part\" or \"word\")? ")
print(boi)
if boi != "house part":
    print("I do not understand", boi +".")
elif boi != "word":
    print("I do not understand", boi + ".")
if boi == "house part":
    hp = input("Please enter a part of a house: ")
    print(hp)
    if hp == "basement":
        print("calf")
    elif hp == "wall":
        print("skin")
    elif hp == "attic":
        print ("hip")
    elif hp == "kitchen":
        print("abdomen")
    elif hp == "study":
        print("wrist")
    else:
        print("I do not know anything about a(n)", hp + ".")
elif boi == "word":
    w = input("Please enter a word: ")
    print(w)
    if w == "attorney":
        print("macilmud")
    elif w == "chicken":
        print("sleent")
    elif w == "consider":
        print("floria")
    elif w == "application":
        print("sailinexemy")
    elif w == "prepare":
        print("capied")
    else:
        print("I do not know anything about a(n)", w + ".")
5
  • @Harith Why do you think that will make any difference? Commented Feb 25, 2021 at 19:12
  • 1
    print("I do not understand", boi +".") is guaranteed to print based on the logic you've given. You probably mean if boi != "house part" and boi != "word": instead of two separate branches. Welcome to SO, BTW. Commented Feb 25, 2021 at 19:12
  • Thank you for the suggestion!! Unfortunately, it didn't work, but I'll keep looking. Thank you! ^^ Commented Feb 25, 2021 at 19:13
  • 1
    @ggorlen, your suggestion worked! Thank you so much! :DD Thanks for the welcome, too! Commented Feb 25, 2021 at 19:15
  • @harith If it's logically the same, the code will run the same. Commented Feb 25, 2021 at 19:15

3 Answers 3

2

All inputs will either be not one or not the other. you need to combine these two into a single condition such as:

if boi not in ("house part","word"):
    print("I do not understand", boi + ".")

or, more simply, add a final else: to the next condition (and remove the first one).

if boi == "house part":
    ...
elif boi == "word":
    ...
else:
    print("I do not understand", boi + ".")
Sign up to request clarification or add additional context in comments.

Comments

1

Your code:

if boi != "house part":
    print("I do not understand", boi +".")
elif boi != "word":
    print("I do not understand", boi + ".")

Entering house part will result in I do not understand house part. as house part is not equal to word which satisfies the elif boi != "word":. It looks like you'd want to combine the two statements into one:

if boi not in ("house part", "word"):

This is because your current code runs like this:

#user enters "house part"
if boi != "house part": 
#boi is equal to "house part" so the if returns 
#false and continues to the elif
    print("I do not understand", boi +".")
elif boi != "word":
#boi is not equal to "word" so the elif is satisfied and 
#the below statements are run.
    print("I do not understand", boi + ".")

Comments

0

when you want validating input, you can use instead of this part in below:

if boi != "house part":
    print("I do not understand", boi +".")
elif boi != "word":
    print("I do not understand", boi + ".")

use this:

if boi != "house part" and boi != "word":
    print("I do not understand", boi +".")

1 Comment

This should be and not or otherwise the issue will remain

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.