Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help debugging
#1
My class has been set a task to calculate the ages of cats and dogs.
Here are the requirements -
The Maths
Cats Equivalent Age
The first 2 years equal 25 Human years every additional year  = 4 Human equivalent years
 
Dogs Equivalent Age
First year equal 15 Human years, the second year is equal to 9 Human years, every additional year  = 4 Human equivalent years
 
 
Task:
Create a program that asks for the type and real age of your pet
Then outputs the Equivalent Human Age for your pet
 
Degrees of Challenge:
 
Basic:
Accepts CAT or DOG and outputs the correct EHA answer
Has a simple HCI (Human Computer Interface) that is followable
 
Advanced:
Uses the pets first name
Rejects other spellings and animals eg RAT, Dog
 
Very Advanced:
Accepts spellings such as Dog, DOG, dOg etc 
Accepts Nonwhole years for the real age
Loops until as acceptable input is given
We are using Python 3.5.2
We are using the website called repl.it


I am going for very advanced and my code is getting an error. This is my code.
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
elif animalType.lower() == "cat":
    #Cat Calculations
    eHA=0
    if animalAge >=1:
        eHA = 12
    if animalAge >=2:
        eHA += 13
    if animalAge >=3:
        eHA += (animalAge-2)*4
elif animalType.lower() = ! "dog" or "cat":
    print("I don't know what an",animalType,"is, Sorry")
print("Your animal is...",eHA,"years old")
#Exit
print("Goodbye")
input("Press ENTER to stop the program")
My code is getting this error.
Error:
Traceback (most recent call last):  File "python", line 21    elif animalType.lower() =! "dog" or "cat":                            ^ SyntaxError: invalid syntax
I don't know how to fix or what the problem even is. Please Help!
Thank You!
Reply
#2
(Mar-26-2017, 04:35 PM)SyntaxError123 Wrote:
elif animalType.lower() = ! "dog" or "cat":

1) There's a space between the equal sign and the bang (and the bang should go before the equal anyway.  Or use double equal sign, and the keyword "not").  =_!
2) This won't throw an error, but that if clause will always evaluate to True.  You should either check for equality against both strings, or create a tuple of the options and check if the type is contained in them.

>>> x = "spam"
>>> x != "spam"
False
>>> x != "spam" or "eggs"
'eggs'
>>> if x != "spam": "yay!"
...
>>> if x != "spam" or "eggs": "yay!"
...
'yay!'
>>> if x not in ("spam", "eggs"): "yay!"
...
>>>
Reply
#3
It's working now thank you!

I am now getting a new error!
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
#4
Duplicate post!
I just answered the other post

Do not post twice
Reply


Forum Jump:

User Panel Messages

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