In: Computer Science
Exercise 6: Dog Years It is commonly said that one human year is equivalent to 7 dog years. However this simple conversion fails to recognize that dogs reach adulthood in approximately two years. As a result, some people believe that it is better to count each of the first two human years as 10.5 dog years, and then count each additional human year as 4 dog years. Write a program that implements the conversion from human years to dog years described in the previous paragraph. Ensure that your program works correctly for conversions of less than two human years and for conversions of two or more human years. Your program should display an appropriate error message if the user enters a negative number.
code by python
Python code:
#accepting number of years
year=int(input("Enter the number of human years: "))
#checking if it negative
if(year<0):
#printing invalid human year
print("Number of human years can't be less than 0")
#checking if year is less than or equal to 2
elif(year<=2):
#printing Number of dog years
print("Number of dog years:",year*10.5)
else:
#printing Number of dog years
print("Number of dog years:",2*10.5+(year-2)*4)
Screenshot:
Input and Output: