Question

For the "dog" part of the code, it works perfectly and does what it's supposed to. However, if you enter "Cat" for the input question at the start, it still goes on and does the dog portion of the code.

Even though I've wrote in the code that if the answer from the question=="Cat"or"cat" then it should do this section not the Dog section.

import time
import sys

animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")

if animal=="Dog"or"dog":
    age=int(input("How old is your Dog?"))
    if age==1:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    elif age==2:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    else:
        age=age-2
        print("Calculating the age of the Dog...")
        time.sleep(1)
        agecalculation=age*4+22
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()

elif animal=="Cat"or"cat":
    age=int(input("How old is your Cat?"))
    if age==1:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 15")
    elif age==2:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 25")
    else:
        age=age-2
        print("Calculating the age of the Cat...")
        time.sleep(1)
        agecalculation=age*4+25
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()
else:
    print("That is not an animal, or isn't on the list specified")
    animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")             
Was it helpful?

Solution

The following if test will always evaluate as true:

if animal=="Dog" or "dog":

To above works as if it had the parentheses:

if (animal=="Dog") or ("dog"):

The second part of the or will, under Python rules, always evaluate to True: non-empty strings have boolean value of True.

Here are three options that work:

if animal=="Dog" or animal == "dog":

if animal in ("Dog", "dog"):

if animal.lower() =="dog":

MORE: These issues can be easily tested on python's handy interactive command prompt. For example, observe the boolean value of "Dog" and "":

>>> bool("Dog"), bool("")
(True, False)

And, here is the combined statement:

>>> bool('Cat' == 'Dog' or 'dog')
True

OTHER TIPS

In python, all no empty string is evaluated as true, as:

if "something":
    print("True")
else
    print("False")

will always print True

So you need to set your if like:

if animal=="Dog" or animal=="dog":
    #Do something with dog
elif animal=="Cat" or animal=="cat":
    #Do something with cat
else:
    #Do something else

You will want to edit your code as such:

import time
import sys

animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")

if animal=="Dog"or animal=="dog":
    age=int(input("How old is your Dog?"))
    if age==1:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    elif age==2:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    else:
        age=age-2
        print("Calculating the age of the Dog...")
        time.sleep(1)
        agecalculation=age*4+22
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()

elif animal=="Cat"or animal == "cat":
    age=int(input("How old is your Cat?"))
    if age==1:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 15")
    elif age==2:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 25")
    else:
        age=age-2
        print("Calculating the age of the Cat...")
        time.sleep(1)
        agecalculation=age*4+25
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()
else:
    print("That is not an animal, or isn't on the list specified")
    animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")             

When you do if animal == "Dog" or "dog", it is evaluating if animal == "Dog", and if "dog". You could use animal.lower() == "dog", or use the edited code above.

The obvious error has already been pointed out by pasztorpisti:

animal=='Cat'or'cat' is not what you wanted. Either use animal.lower()=='cat' or animal in ['cat', 'Cat'] or another approach.

I also think that your whole code could be trimmed down quite a bit. Maybe you can grab one or two ideas from the following snippet:

def dogAge(age):
    return 11 if age < 3 else age * 4 + 14

def catAge(age):
    if age == 1: return 15
    if age == 2: return 25
    return age * 4 + 17

animal = input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog").lower()
if animal not in ['cat', 'dog']:
    print("That is not an animal, or isn't on the list specified")
    sys.exit(0)

age = int(input("How old is your {}?".format(animal)))

animalAge = {'cat': catAge, 'dog': dogAge}[animal](age)

print("The age of the animal is {}.".format(animalAge))

I suspect (without having checked it) that the problem isn't your logic but your conditions.

animal == "Dog" or "dog" evaluates as (animal == "Dog") or "dog" which will always evaluate to True since "dog" is a non-null string. Try instead animal == "Dog" or animal == "dog", or better still what about animal in ("Dog", "dog") or animal.lower() == "dog"?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top