Python homework help
Author
Discussion

boyse7en

Original Poster:

8,207 posts

195 months

Thursday 9th July 2020
quotequote all
As part of my new job of home tutor for my kids, I've been dropped in the deep end with some python homework. I have zero programming knowledge since Sinclair Basic about 30 years ago, so I'm getting a bit lost.

Daughter has to write a program which calculates someones age in days based on their inputs of age in years/months/days.

So far we've managed to come up with this:

print ("Age in days calculator")
yearage = int(input("Please enter your age in years"))
monthage = int(input("and the number of months"))
dayage = int(input("and the number of days"))
ydayage = (yearage*365)
mdayage = (monthage*30)
totalage =(mdayage + ydayage + dayage)
print ("You are", totalage, "days old")

Which works fine, but fails badly if someone enters a non-integer as part of their age.

How can we add in a simple "Please enter a whole number" message if any of the yearage, monthage or dayage are not integers?

I've tried all sorts but just doesn't seem to work.

xeny

5,524 posts

108 months

Thursday 9th July 2020
quotequote all
Where are you stuck, checking the no is an integer, or the loop back to get the number re-entered?

vourin

29 posts

230 months

Thursday 9th July 2020
quotequote all
What about leap years? And months with more or less than 30 days? In fact there is a function you could use, timedelta - https://docs.python.org/2/library/datetime.html

Edited by vourin on Thursday 9th July 14:07

frisbee

5,623 posts

140 months

Thursday 9th July 2020
quotequote all
It works ok if I run it (python 2.7.8).

int() will attempt to convert whatever you enter into an integer. A decimal number will have the decimal part removed, text will give an error.


boyse7en

Original Poster:

8,207 posts

195 months

Thursday 9th July 2020
quotequote all
xeny said:
Where are you stuck, checking the no is an integer, or the loop back to get the number re-entered?
If the user enters a non-integer into any of the three inputs it throws up a error saying:
"Traceback (most recent call last):
File "python", line 2, in <module>
ValueError: invalid literal for int() with base 10: '13.2'"

which isn't very user friendly. I'm trying to get it to Print ("Sorry, please enter a whole number") and then ask again for the input

boyse7en

Original Poster:

8,207 posts

195 months

Thursday 9th July 2020
quotequote all
vourin said:
What about leap years? And months with more or less than 30 days?
Not interested in that level of precision. It's only a homework assignment for a 13yo smile

boyse7en

Original Poster:

8,207 posts

195 months

Thursday 9th July 2020
quotequote all
frisbee said:
It works ok if I run it (python 2.7.8).

int() will attempt to convert whatever you enter into an integer. A decimal number will have the decimal part removed, text will give an error.
It works ok for me if I enter an integer (eg; 13) but I get an error message and the program stops if I enter a non-integer (eg 13.2)
I'd like it to loop back to ask for a whole number

xeny

5,524 posts

108 months

Thursday 9th July 2020
quotequote all
boyse7en said:
If the user enters a non-integer into any of the three inputs it throws up a error saying:
"Traceback (most recent call last):
File "python", line 2, in <module>
ValueError: invalid literal for int() with base 10: '13.2'"

which isn't very user friendly. I'm trying to get it to Print ("Sorry, please enter a whole number") and then ask again for the input
How about using floats, testing if the input is an integer with is_integer() ,and if it isn't, asking for another try on a loop?



eharding

14,648 posts

314 months

Thursday 9th July 2020
quotequote all
Adapt the following for all of your required values. You'll get extra marks for demonstrating the use of functions and exception handling.


def getIntegerValue(promptStr):
while True:
try:
return int(input(promptStr))
except ValueError:
print("Please enter an integer value")

age = getIntegerValue("Age: ")

frisbee

5,623 posts

140 months

Thursday 9th July 2020
quotequote all
boyse7en said:
frisbee said:
It works ok if I run it (python 2.7.8).

int() will attempt to convert whatever you enter into an integer. A decimal number will have the decimal part removed, text will give an error.
It works ok for me if I enter an integer (eg; 13) but I get an error message and the program stops if I enter a non-integer (eg 13.2)
I'd like it to loop back to ask for a whole number
I'm guessing you're using Python 3.

You could try int(float(input("Please enter your age in years"))) as the dirty fix.

boyse7en

Original Poster:

8,207 posts

195 months

Thursday 9th July 2020
quotequote all
eharding said:
Adapt the following for all of your required values. You'll get extra marks for demonstrating the use of functions and exception handling.


def getIntegerValue(promptStr):
while True:
try:
return int(input(promptStr))
except ValueError:
print("Please enter an integer value")

age = getIntegerValue("Age: ")
Thanks, that'll give us something to chew on! [head's off to google exception handling...]

essayer

10,410 posts

224 months

Thursday 9th July 2020
quotequote all
Use datetime to calculate the difference between today and the date they entered.
Don’t try and do the maths yourself as you’ll have to think about leap years, etc

Shamelessly stolen from stackoverflow, like all my best work:
>>> import datetime
>>> today = datetime.date.today()
>>> someday = datetime.date(2008, 12, 25)
>>> diff = someday - today
>>> diff.days
86

Edited by essayer on Thursday 9th July 14:38

toohuge

3,501 posts

246 months

Friday 10th July 2020
quotequote all
There are many ways to do this - as there always is in programming. But the clue is in the question and the answers above this using datetime is what is required.

The question appears to be assessing the students use of datetime but a) entering two dates and b) performing a calculation on these inputs.

Brads67

3,199 posts

128 months

Friday 10th July 2020
quotequote all
I thought this was about Cleese etc. smile

frisbee

5,623 posts

140 months

Friday 10th July 2020
quotequote all
toohuge said:
There are many ways to do this - as there always is in programming. But the clue is in the question and the answers above this using datetime is what is required.

The question appears to be assessing the students use of datetime but a) entering two dates and b) performing a calculation on these inputs.
The first post is asking for an age to be entered, rather than dates. That would suggest that only a crude calculation is required rather than using datetime.

smashing

1,613 posts

191 months

Friday 10th July 2020
quotequote all
essayer said:
Use datetime to calculate the difference between today and the date they entered.
Don’t try and do the maths yourself as you’ll have to think about leap years, etc

Shamelessly stolen from stackoverflow, like all my best work:
>>> import datetime
>>> today = datetime.date.today()
>>> someday = datetime.date(2008, 12, 25)
>>> diff = someday - today
>>> diff.days
86

Edited by essayer on Thursday 9th July 14:38
+1

Condi

20,492 posts

201 months

Friday 10th July 2020
quotequote all
Depressing when a 13 year old has better programming skills than me. frown

toohuge

3,501 posts

246 months

Saturday 11th July 2020
quotequote all
frisbee said:
The first post is asking for an age to be entered, rather than dates. That would suggest that only a crude calculation is required rather than using datetime.
Has the op been edited since ? As in the op it asks to calculate the difference in age in days after entering year/month/day ....

Dr Jekyll

23,820 posts

291 months

Saturday 11th July 2020
quotequote all
toohuge said:
frisbee said:
The first post is asking for an age to be entered, rather than dates. That would suggest that only a crude calculation is required rather than using datetime.
Has the op been edited since ? As in the op it asks to calculate the difference in age in days after entering year/month/day ....
Which isn't sufficient information unless it's only supposed to run on one specific date. Someone one year and one month old could be anything from 393 days old to 397.

Mr Whippy

32,453 posts

271 months

Saturday 11th July 2020
quotequote all
This is amazingly odd homework.

You either.

Look up a function of python. If the homework is to learn how to use Google and determine if Python has a function for that task, then that is your 'programming' homework done.

Or you understand the programming logic. And ideally exhibit understanding the pseudo code, ideally by drawing a flow diagram to show the problem and how you'd solve it.


If homework is the former that is pretty poor.

If it's the latter then I can see how that develops the thinking skills intrinsic to programming, and likely in 10-15 years, intrinsic to being able to get the most from the AI that'll do the actual coding for you based on your design.