Attempting to solve “how old is your dog in human years?” in python on coursera

Question:

I keep running into a problem that I am unable to understand. Can someone please help me understand what it means. just need a hint if that’s ok. I have attempted to google the error I have attempted asking my coworkers and my last resort is the stack overflow community. so far the only thing I was able to make out is the my print is incorrectly formatted but how I cannot understand.
Here is my code:

def calculator():
    age = input("Please enter dog age ")
    d_age = 0
    try:
        d_age = float(age)
        if d_age <= 0:
            raise NameError
        elif d_age >= 5:
            d_age = ((d_age - 5) * 7 + 36)
          
        elif d_age <= 1:
            d_age = (d_age * 15)
            
        elif d_age <= 2:
            d_age = (d_age * 12)
            
        elif d_age <= 3:
            d_age = (d_age * 9.3)
           
        elif d_age <= 4:
            d_age = (d_age * 8)
            
        elif d_age <= 5:
            d_age = (d_age * 7.2)

    except ValueError:
        print("entered age is invalid!")
    except NameError:
        print("Please enter a non negative age")
    print("The given dog age", age, "is", round(d_age, 2), "in human years.")

calculator()
Asked By: serge

||

Answers:

Try this. Previously, if you inputted "1" if d_age <= 1: would be true, and if d_age <= 2: would be true, and so on so forth. This only allows one condition to be true which gives the correct answer.

Your second sneakier issue is that the course is assuming your calculator function does not take any arguments, but rather calls input from inside the function.

def calculator():
    age = input("Please enter dog age ")
    d_age = 0
    try:
        d_age = float(age)
        if d_age <= 0:
            raise NameError
        elif d_age >= 5:
            d_age = ((d_age - 5) * 7 + 36)
          
        elif d_age <= 1:
            d_age = (d_age * 15)
            
        elif d_age <= 2:
            d_age = (d_age * 12)
            
        elif d_age <= 3:
            d_age = (d_age * 9.3)
           
        elif d_age <= 4:
            d_age = (d_age * 8)
            
        elif d_age <= 5:
            d_age = (d_age * 7.2)

    except ValueError:
        print("entered age is invalid!")
    except NameError:
        print("Please enter a non negative age")
    print("The given dog age", age, "is", round(d_age, 2), "in human years.")

calculator()
Answered By: JRose
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.