Python Forum

Full Version: More Errors. Less Solutions.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am now getting a new errors and or my code it not working rather!
animalType = input("What sort of animal do have, DOG or CAT? \n>")
animalAge = int(input("How old is your "+animalType+" in whole years? \n>"))
if animalType.lower() == "dog":
    #Dog Calculations
    eHA=0
    if animalAge >=1:
        eHA = 15
    if animalAge >=2:
        eHA += 9
    if animalAge >=3:
        eHA += (animalAge-2)*4
if animalType.lower() == "cat":
    #Cat Calculations
    eHA=0
    if animalAge >=1:
        eHA = 12
    if animalAge >=2:
        eHA += 13
    if animalAge >=3:
        eHA += (animalAge-2)*4
while animalType.lower() != "dog" or "cat":
    print("I don't know what a",animalType,"is, Sorry. Try Again")
    animalType = input("What sort of animal do have, DOG or CAT?")
print("Your animal is...",eHA,"years old")
#Exit
print("Goodbye")
input("Press ENTER to stop the program")
This is my output
Output:
What sort of animal do have, DOG or CAT?  > rat How old is your rat in whole years?  > 10 I don't know what a rat is, Sorry. Try Again What sort of animal do have, DOG or CAT? cat I don't know what a cat is, Sorry. Try Again What sort of animal do have, DOG or CAT? 
If you dont know whats wrong, my loop - 
while animalType.lower() != "dog" or "cat":
    print("I don't know what a",animalType,"is, Sorry. Try Again")
    animalType = input("What sort of animal do have, DOG or CAT?")
is not working as intended. It is ment to check for animals that are not dogs or cats but even when they are dogs or cats its still runs the loop! Please help!!  Wall
change code:

animalType = None
while animalType != "dog" and animalType != "cat":
    animalType = input("What sort of animal do have, DOG or CAT? \n>").lower()
    animalAge = int(input("How old is your "+animalType+" in whole years? \n>"))
    if animalType == "dog":
        #Dog Calculations
        eHA=0
        if animalAge >=1:
            eHA = 15
        if animalAge >=2:
            eHA += 9
        if animalAge >=3:
            eHA += (animalAge-2)*4
    elif animalType == "cat":
        #Cat Calculations
        eHA=0
        if animalAge >=1:
            eHA = 12
        if animalAge >=2:
            eHA += 13
        if animalAge >=3:
            eHA += (animalAge-2)*4
    if animalType != "dog" and animalType != "cat":
        print("I don't know what a",animalType,"is, Sorry. Try Again")
        # animalType = input("What sort of animal do have, DOG or CAT?")
print("Your animal is...",eHA,"years old")
#Exit
print("Goodbye")
input("Press ENTER to stop the program")
Nilanmo This is a new issue
Well, lets take animalType.lower() != "dog" or "cat" and see what is happening here.
AnimalType.lower() != "dog" is evaluated to True or False. It's not important because the next logical operator is or. In Python a non enpty variable is always True ( if it's not a boolean ). So "cat" == True. (AnumalType.lower() != "dog") (False, True - doesn't matter ) or "cat" ( which is always True ) is True in any case. And the while loop is executed no matter if the anumal is 'dog or 'rat' or 'elephant'.
(Mar-30-2017, 07:17 PM)SyntaxError123 Wrote: [ -> ]Nilanmo This is a new issue

It runs all the time, without regard to what the user types in. It is, in fact, the exact same error from your other thread, that has already been answered several times.
(Mar-26-2017, 07:01 PM)micseydel Wrote: [ -> ]For a little more explanation: https://python-forum.io/Thread-Multiple-...or-keyword