##### Interesting Intro to python problem I can't solve.
 Interesting Intro to python problem I can't solve. Honestworker Unladen Swallow Posts: 2 Threads: 1 Joined: Feb 2021 Reputation:   Feb-26-2021, 04:04 PM (This post was last modified: Feb-26-2021, 04:15 PM by buran.) Can anyone solve this problem? “How Old is Your Dog in Human Years?” Calculator 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'. Use python to code it. ```import traceback def calculator(): # Get dog age dog_age = input("Enter your dog's age ") try: # Cast to float dog_age = float(dog_age) except: print(dog_age, "is an invalid age.") print(traceback.format_exc()) calculator()``` buran write Feb-26-2021, 04:15 PM:Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you. See BBcode help for more info. Reply Posts: 7,481 Threads: 130 Joined: Sep 2016 Reputation: Feb-26-2021, 04:17 PM (This post was last modified: Feb-26-2021, 04:17 PM by buran.) Yeah, interesting homework... We are glad to help, but we are not going to do your homework. You need to put some real effort yourself. If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply Honestworker Unladen Swallow Posts: 2 Threads: 1 Joined: Feb 2021 Reputation: Feb-26-2021, 04:27 PM (This post was last modified: Feb-26-2021, 04:27 PM by Honestworker.) (Feb-26-2021, 04:17 PM)buran Wrote: Yeah, interesting homework... We are glad to help, but we are not going to do your homework. You need to put some real effort yourself. It is actually not homework but a free course on Coursera. Im 48 years of age and havent done homework for a very long time. and my effort is below..... ```import traceback def calculator(): # Get dog age dog_age = input("Enter the dog's age:") try: dog_age = float(dog_age) dog_age_in_Human_years = 0 if dog_age < 0 : flag = True elif 0 < dog_age <= 1 : dog_age_in_Human_years += dog_age * 15.0 elif 1 < dog_age <= 2 : dog_age_in_Human_years += dog_age * 12.0 elif 2 < dog_age <= 3 : dog_age_in_Human_years += dog_age * 9.3 elif 3 < dog_age <= 4 : dog_age_in_Human_years += dog_age * 8.0 elif 4 < dog_age <= 5 : dog_age_in_Human_years += dog_age * 7.2 else: dog_age_in_Human_years += 5 * 7.2 + (dog_age - 5) * 7.0 except: print(dog_age, "is an invalid age.") print(traceback.format_exc()) if flag == False : print("The given dog age", dog_age, "is", round(dog_age_in_Human_years, 2), "in human years.") else: print("Age cannot be a negative number") calculator()``` Reply deanhystad Da Bishop Posts: 1,927 Threads: 10 Joined: Feb 2020 Reputation: Feb-26-2021, 05:29 PM I'm a lot older than 48 and I am doing homework right now. I figure I'll stop doing homework a few minutes before I stop breathing. Your "effort" crashes because flag is only defined when the age entered < 0. I would replace this: ```if dog_age < 0 : flag = True```with ```if dog_age < 0: raise ValueError```You already have an exception handler, you may as well use it. You can also simplify the comparisons and your math: ``` if dog_age < 0 : raise ValueError elif dog_age <= 1 : dog_age_in_Human_years = dog_age * 15.0 elif dog_age <= 2 : dog_age_in_Human_years = dog_age * 12.0``` GOTO10 likes this post Reply Posts: 7,481 Threads: 130 Joined: Sep 2016 Reputation: Feb-26-2021, 07:54 PM (Feb-26-2021, 04:27 PM)Honestworker Wrote: It is actually not homework but a free course on Coursera. Im 48 years of age and havent done homework for a very long time. homework is not only school "thing". If you prefer let's call it assignment. If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply BashBedlam Lumberjack Posts: 136 Threads: 0 Joined: Jan 2021 Reputation: Mar-04-2021, 02:05 AM If it were me, I'd use a lookup table like this : `human_years_lookup = [0, 15, 24, 27.9, 32, 36]`and then anything over 5 years is 36 + (age_of_dog -5) * 7 Reply

 Possibly Related Threads… Thread Author Replies Views Last Post How to solve problem 1181 of the URI with numpy. thiagohps 4 217 Mar-22-2021, 04:11 PM Last Post: buran solve probability problem by python Dreammer 1 302 Dec-24-2020, 09:51 AM Last Post: Larz60+ Intro python using lists Rustypotatoes 2 458 Sep-17-2020, 08:30 PM Last Post: jefsummers An interesting Role-Play-Game battle script NitinL 4 1,140 Apr-02-2020, 03:51 AM Last Post: NitinL Algorithm to solve a case of Travelling Salesman Problem Ale888 3 1,011 Dec-11-2018, 07:49 PM Last Post: buran Cross word puzzle solve using python constraint library aliyark145 1 1,309 Nov-29-2018, 10:53 AM Last Post: Larz60+ Intro to Python for Data Science with Datacamp Sabrusura 4 1,538 Aug-21-2018, 09:03 AM Last Post: Sabrusura how to solve this problem. IndexError: axis 2 out of bounds [0, 2) bedio 10 4,846 Nov-21-2017, 02:47 PM Last Post: heiner55

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020