Python Forum
More Errors. Less Solutions.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
More Errors. Less Solutions.
#1
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
Reply
#2
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")
Reply
#3
For a little more explanation: https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#4
Your issue was already answered. https://python-forum.io/Thread-Need-help...4#pid13364
Reply
#5
Nilanmo This is a new issue
Reply
#6
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'.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(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.
Reply
#8
(Mar-26-2017, 07:01 PM)micseydel Wrote: For a little more explanation: https://python-forum.io/Thread-Multiple-...or-keyword
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compute complex solutions in quadratic equations liam 1 1,846 Feb-09-2020, 04:18 PM
Last Post: Gribouillis
  Solutions to this questions jerrykatoh 8 115,071 Feb-23-2017, 11:59 AM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020