Python homework help
Discussion
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.
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.
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
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
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)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.
I'd like it to loop back to ask for a whole number
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?"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
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: ")
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)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.
I'd like it to loop back to ask for a whole number
You could try int(float(input("Please enter your age in years"))) as the dirty fix.
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.
Thanks, that'll give us something to chew on! [head's off to google exception handling...]
def getIntegerValue(promptStr):
while True:
try:
return int(input(promptStr))
except ValueError:
print("Please enter an integer value")
age = getIntegerValue("Age: ")
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
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
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 question appears to be assessing the students use of datetime but a) entering two dates and b) performing a calculation on these inputs.
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.The question appears to be assessing the students use of datetime but a) entering two dates and b) performing a calculation on these inputs.
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
+1Don’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
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 ....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 ....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.
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.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff




