Need help debugging - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Need help debugging (/thread-2581.html) |
Need help debugging - SyntaxError123 - Mar-26-2017 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. I don't know how to fix or what the problem even is. Please Help!Thank You! RE: Need help debugging - nilamo - Mar-26-2017 (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!" ... >>> RE: Need help debugging - SyntaxError123 - Mar-26-2017 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 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!! RE: Need help debugging - Larz60+ - Mar-26-2017 Duplicate post! I just answered the other post Do not post twice |