[Repost] What Would You Weigh On Mars?

[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]

1995561-mars_attacks_1_1024x768

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.

plutodog

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…

w3

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.

w2

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.

w1

Seriously, just go to Mars you’ll be fine.

Code with comments, followed by copy-paste code is all below. Based on two examples found here and here.

If you want to know your weight and age on other planets RIGHT NOW (because it’s awesome) click here and here   .

w10

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”)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s