'Convert dog ages to human years

it as been a long time without programming. I'm doing a function that converts the age of a dog to human years, but I'm getting some mistakes and I am not getting through them. This is my code

def calculator():

    # Get dog age
    age = input("Input dog years: ")

    try:
        # Cast to float
        d_age = float(age)
        # If user enters negative number, print message
        if(d_age < 0):
            print("Age can not be a negative number", age)
        # Otherwise, calculate dog's age in human years
        elif(d_age == 1):
            d_age = 15
        elif(d_age == 2):
            d_age = 2 * 12
        elif(d_age == 3):
            d_age = 3 * 9.3
        elif(d_age == 4):
            d_age = 4 * 8
        elif(d_age == 5):
            d_age = 5 * 7.2
        else:
            d_age = 5 * 7.2 + (float(age) - 5) * 7
        print("\n \t \'The given dog age", age, "is", d_age, "human years.'")
    except ValueError:
            print(age, "is an invalid age")

calculator()

and I'm not understanding why d_age < 0 is not working and I'm not getting why it appears as an error "calculator() is not defined". Thanks in advance!

NOTE: I saw that there already is question about this, but I'm doing in a different way.

EDIT: I just didn't do it with dictionaires, because it wasn't yet introduced in the course. I'm just trying to do with what I learned. Thanks anyway.



Solution 1:[1]

Looks like your code is not structure correctly. If you move the elif blocks and make some minor changes it will work. See below:

def calculator():

    # Get dog age
    age = input("Input dog years: ")

    try:
        # Cast to float
        d_age = float(age)
        # If user enters negative number, print message
        if(d_age < 0):
            print("Age can not be a negative number", age)
        # Otherwise, calculate dog's age in human years
        elif(d_age == 1):
            d_age = 15
        elif(d_age == 2):
            d_age == 2 * 12
        elif(d_age == 3):
            d_age == 3 * 9.3
        elif(d_age == 4):
            d_age = 4 * 8
        elif(d_age == 5):
            d_age = 3 * 7.2
        else:
            d_age = 5 * 7.2 + (age - 5) * 7
        print("\n \t \'The given dog age", age, "is", d_age, "human years.")
    except ValueError:
            print(age, "is an invalid age")

calculator()

Solution 2:[2]

How about this?

  • While statement to keep question looping until you get a valid response.
  • Dictionary to have a more organised age calculation sheet instead of lots of if statements.

def calculator_dog_age():

    age = 0
    while age == 0:
        age = input("Input dog years: ")
        try:
            age = int(age)
            if 20 > age > 0:
                print('A good age was entered:', age)
                age_dict = {1: 15, 2: 24, 3: 27.9, 4: 32}
                if age in age_dict:
                    return age_dict[age]
                else:
                    return age * 7.2 + (age - 5) * 7
            else:
                print('Please enter a realistic age!')
                age = 0
        except:
            print('Please enter an integer!')
            age = 0


print(calculator_dog_age())

Solution 3:[3]

This is my suggestion, even though the answer was accepted.

def calculator():
    age = input("Input dog years: ")
    try:
        d_age = float(age)
        ageDict = {1: 15, 2: 12, 3: 9.3, 4: 8, 5: 7.2}
        if d_age in ageDict:
            print(round(d_age*ageDict[d_age],2))
        else:
            print(round(5*7.2+(d_age - 5)*7,2))
    except ValueError:
        print(age,"is an invalid age")

calculator()

Solution 4:[4]

#get user's input on dog_age
dog_age = input("Enter your dog's age in years")
try:
    #cast to a float
    d_age = float(dog_age)
    #convert dog age to human age
    #ensure input is not negative
    if (d_age > 0):
        if (d_age <=1):
            h_age = d_age *15
        elif (d_age <=2):
            h_age = d_age *12
        elif (d_age <=3):
            h_age = d_age*9.3
        elif (d_age <=4):
            h_age = d_age*8
        elif (d_age <=5):
            h_age = d_age*7.2
        else:
            h_age = 36 + (d_age - 5) *7    
        human_age = round(h_age,2)
        #print instruction for valid input
        print('The given dog age', dog_age,'is', str(human_age),'in human years')
    else: 
        print('Age cannot be a negative number.')
except ValueError:
    print(dog_age,'is invalid.')

The question I was solving has similar conditions but with different calculations required. I hope it helps (P.S initially I also could not figure out why my dog age <0 condition was not working, turns out, it’s all about blocking the conditions together and indentation use (: )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sefan
Solution 2 Ruben
Solution 3 user56700
Solution 4 Tiffany