Question
write a program that calculates a dog's age in human years. The program will prompt the user for an age in dog years and calculate
write a program that calculates a dog's age in human years. The program will prompt the user for an age in dog years and calculate that age in human years. Allow for int or float values, but check the user's input to make sure it's valid -- it should be numeric and positive. Otherwise, let the user know their input is not valid.
You can use the following rules to approximately convert a medium-sized dog's age to human years:
For the first year, one dog year is equal to 15 human years
For the first 2 years, each dog year is equal to 12 human years
For the first 3 years, each dog year is equal to 9.3 human years
For the first 4 years, each dog year is equal to 8 human years
For the first 5 years, each dog year is equal to 7.2 human years
After that, each dog year is equal to 7 human years. (Note: This means the first 5 dog years are equal to 36 human years (5 * 7.2) and the remaining dog years are equal to 7 human years each.)
Print the result in the following format, substituting for dog_age and human_age: "The given dog age dog_age is human_age in human years." Round the result to 2 decimal places. Note: If there is a 0 in the hundredths place, you can drop it, e.g. 24.00 can be displayed as 24.0.
Considering invalid inputs:
Your program must ask the user for an age in dog years - hint: use the input() function
We are going to test invalid inputs - make sure that your code can handle negative value inputs and non-numerical inputs!
For invalid inputs, make sure that your printed response adheres to the following:
If a text-based input is provided, make sure your response contains the word 'invalid'.
If a negative input is provided, make sure your response contains the word 'negative'.
A note on defining functions
Defining a function refers to the act of creating a function using the def keyword and giving it a name, as well as any arguments that need to be available in the body of the function. We'll see more about functions later in the course, but below the def calculator(): line is the beginning of a function declaration, with all of the indented lines below it as the body of the function.
Similar to the way we name variables, a function may not contain spaces or special characters, with the exception of the underscore (_). Below are some examples of functions being defined.
def say_hello():
print("Hello!")
The above function named say_hello prints the word "Hello!". It takes in no arguments - that is, there is nothing being passed into the function inside of the parentheses that would be available to the body of the function. Let's look an another example that does contain an argument:
def say_something_specific(thing_to_say):
print(thing_to_say)
Here we have a function named say_something_specific with an argument thing_to_say. The argument could be anything (whenever we decide to call this function, we'll pass in whatever we'd like at the time we call it) and will be printed out similar to the way "Hello!" was printed in the first function.
def number_sum(num1, num2):
sum = num1 + num2
print("Sum is", sum)
return sum
This function, number_sum, takes in two arguments separated by commas, named num1 and num2. Arguments are essentially variables that are accessible throughout the function body. In this case, a variable sum is created with a value equal to the sum of our two arguments, num1 and num2. We then print out "Sum is ", then finally return the sum. The return allows us to get a value out of the function after it's done executing. Whatever follows the return keyword will be sent back to the location in your code where the function was called. Let's take a look at one more example of how a function could be defined, then used in code. We'll use the function that we just created, number_sum.
# ...
a = 5
b = 3
sum_a_b = number_sum(a, b)
# At this point in the code, number_sum returned a (5) + b (3) = 8, so the value of
# sum_a_b = 8
sum_x_y = number_sum(10, 20)
# After the above line is executed, sum_x_y will be equal to 30
This is my answer but I keep getting calculation errors.. saying calculation is not defined
import traceback
def calculator():
age = input("Input dog years: ")
try:
d_age = float(age)
h_age = float()
if d_age >0:
#if user enters negative number, print message
if d_age <=1:
h_age = d_age * 14
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
h_age = round(h_age, 2)
print('The given dog age ' + str(d_age) + 'is' + str(h_age) + 'in human years')
else:
print("Age cannot be negative or zero.")
except:
print(age, "is an invalid age.")
print(traceback.format_exc())
calculator(d_age)
Step by Step Solution
3.40 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
Revised Code python impor...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started