[This was originally posted last year, on an old blog to promote computational thinking and programming in schools. I’ll be reposting these over the next month as I think they’re still useful to schools embarking on the new Computer Science course]
This week I’ve been exploring a couple of new concepts in Python (new… to me). The main one is a big one. Python has a lot of pre-created code for bits and bobs that crop up often for programmers. This is both majorly helpful and also a bit of a pain. Helpful because it stops you having to work out simple ideas using complex code (as you’ll see below), but a bit annoying in that you have to find it first.
The code I’ve developed prompts users to enter their birthday and weight, and then displays their equivalent ages and weights across the different planets in the solar system. Pluto isn’t technically a planet these days, but we’ll keep it regardless.
Pluto, you suck.
So the program doesn’t use many features I haven’t covered before, but the first line shows that I’ve imported ‘date’ from a bank of code called ‘datetime’. This code allows for basic manipulation of dates without having to do all the hideous conversions from integers, to strings, to calculating days from months blah blah blah. Useful.
It gets used in this example by taking two dates, finding the difference between them and giving back an integer that is essentially the number of days you have been alive (give or take a little).
The program then takes that number, which obviously is different for every user (hence the inputs at the start of the program), and performs basic calculations on it based on values supplied helpfully by smarter people than me, here.The values basically being the different lengths of years per planet measured in earth days.
The program also takes the user’s weight, and does much the same thing with the number, albeit with values relevant to the different surface gravity on each planet (which I found here).
Funnily enough you’re a lot younger on some planets than others…
Not sure this is quite how it works.
Numbers had to be ‘float’ rather than ‘int’ because ‘int’ only covers whole numbers, and we need decimals (otherwise your age will likely be less than 1 for some of the planets, and hence appear as a series of zeros … boring). However ‘float’ displays a lot of decimal places, so I also had to use the ’round’ function to get them a little more sensible.
Hopefully students can figure out for themselves that it makes sense that you’d be ‘younger’ on planets further away, as they take longer to get around the sun.
The weight calculation just forms part of the ‘print’ function rather than being separate variables just to make the code shorter.
Seriously, just go to Mars you’ll be fine.
from datetime import date print (“Please enter your birthday “) bd_y=int(input(“Year:”)) bd_m=int(input(“Month (1-12):”)) bd_d=int(input(“Date:”)) weight = float(input(“what is your weight (any units)? “)) units = input(“What units did you use? “) now = date.today () age = date(int(bd_y), int(bd_m), int(bd_d)) delta = now – age earth = round(float(delta.days/365.26),2) mercury = round(float(delta.days/87.97),2) venus = round(float(delta.days/224.7),2) mars = round(float(delta.days/(365.26*1.88)),2) jupiter = round(float(delta.days/(365.26*11.86)),2) saturn = round(float(delta.days/(365.26*29.46)),2) uranus = round(float(delta.days/(365.26*84.01)),2) neptune = round(float(delta.days/(365.26*164.79)),2) pluto = round(float(delta.days/(365.26*248.59)),2) print (“on earth you are”, earth, ” years old and weigh “, weight, ” “, units,”\n”) print (“on mercury you are”, mercury, “years old and weigh “, weight*176, ” “, units,”\n”) print (“on venus you are”, venus, “years old and weigh “, weight*117, ” “, units,”\n”) print (“on mars you are”, mars, “years old and weigh “, weight*1.03, ” “, units,”\n”) print (“on jupiter you are”, jupiter, “years old and weigh “, weight*0.41, ” “, units,”\n”) print (“on saturn you are”, saturn, “years old and weigh “, weight*0.43, ” “, units,”\n”) print (“on uranus you are”, uranus, “years old and weigh “, weight*0.75, ” “, units,”\n”) print (“on neptune you are”, neptune, “years old and weigh “, weight*0.67, ” “, units,”\n”) print (“on pluto you are”, pluto, “years old and weigh “, weight*0.58, ” “, units,”\n”)