0

While writing a project for fun, I came across an unfamiliar issue with If and Elif statements. For some reason when the user inputs a dog "type", python ignores the if statements and continues on using the rest of the statement. The program was written in Python 3.6. I have no clue as to why the if and elif statements aren't working properly. Is there a formatting/syntax issue that I am not aware of? Thanks in advance!

def small():
    small_list = [0, 15, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80]
    small_input = input("How old is your dog?")
    print(small_input)
    small_age = int(small_input)
    small_set2 = small_list[small_age]
    print("Your dog's age is: " + str(small_set2))


def medium():
    medium_list = [0, 15, 24, 28, 32, 36, 42, 47, 51, 56, 60, 65, 69, 74, 78, 83, 87]
    medium_input = input("How old is your dog?")
    print(medium_input)
    medium_age = int(medium_input)
    medium_set2 = medium_list[medium_age]
    medium_set2 = str(medium_set2)
    print("Your dog's age is: " + str(medium_set2))


def large():
    large_input = input("How old is your dog?")
    print(large_input)
    large_set = [0, 15, 24, 28, 32, 36, 45, 50, 55, 61, 66, 72, 77, 82, 88, 93, 120]
    large_age = int(large_input)
    large_set2 = large_set[large_age]
    large_set2 = str(large_set2)
    print("Your dog's age is: " + str(large_set2))


def dog():
    dog1 = input('What size dog do you have?')
    print(dog1)
    if dog1 == 'Small' or 'small':
        print("Okay, you have a small dog.")
        small()
    elif dog1 == 'Medium' or 'medium':
        print("Okay, you have a medium dog.")
        medium()
    elif dog1 == 'Large' or 'large':
        print("Okay, you have a large dog.")
        large()
    else:
        print("Sorry, I did not understand that.")
        dog()


dog()
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • 4
    `if dog1 == 'Small' or 'small':` is the same as `if (dog1 == 'Small') or ('small'):`. As `'small'` is "truthy", this will always be true. The proper way to do this is `if dog1 in ('Small', 'small')` or `if dog1.lower() == 'small'` – Patrick Haugh Oct 09 '17 at 23:55
  • 1
    Please format your code. Python is very sensitive to whitespace, and your code immediately raises a syntax error when run as posted. – Mad Physicist Oct 09 '17 at 23:56
  • 3
    @patrick. Or at least `if dog == 'small' or dog == 'Small'`. Also, `casefold` is a nice alternative to `lower`. – Mad Physicist Oct 09 '17 at 23:59
  • 1
    Hi @TyrannousHail03, welcome to StackOverflow :) Your question could be made shorter and more clear with a better example. You could isolate the issue down to a specific not matching value and the `if` statement on your own, which is all you need to show. Please read https://stackoverflow.com/help/mcve so your future questions are better. – viraptor Oct 09 '17 at 23:59
  • You probably don't want to use recursion to handle invalid input. This is what happens when you call `dog()` in the `else` clause. Use a loop instead and break out of it when complete. – mhawke Oct 10 '17 at 00:01
  • @MadPhysicist I didn't know about `casefold`, thanks! – Patrick Haugh Oct 10 '17 at 00:01
  • @mhawke. Please do not make syntactic changes to code in the future, no matter how "obvious" it may seem to you. This is Python, so the change you made is definitely not purely cosmetic in nature. – Mad Physicist Oct 10 '17 at 00:07
  • @patrick. it's a bit slower than lower and doesn't give you any real advantage when the target is in plain English, but it is very nice when you deal with certain foreign languages. – Mad Physicist Oct 10 '17 at 00:11
  • @MadPhysicist, my code was formatted before I posted it here... Sorry for not catching the formatting in the post... I know for sure formatting isn't the problem. I will try a version with casefold, thanks! – TyrannousHail03 Oct 10 '17 at 00:18
  • @MadPhysicist: Really? Thanks for your advice, but I think that I'll continue to use my common sense in future code edits. – mhawke Oct 10 '17 at 00:26

1 Answers1

1
if dog1 == 'Small' or 'small':

This statement parses as:

if (dog1 == 'Small') or ('small'):

Because non-empty strings are always truthy, this is equivalent to:

if (dog1 == 'Small') or True:

So the condition is always met.

You probably want this instead:

if dog1 in ('Small', 'small'):

Or even better (ignore all the capitalisation issues):

if dog1.lower() == 'small':
viraptor
  • 33,322
  • 10
  • 107
  • 191