Did you know that Alpha-Centauri was the second nearest star to planet Earth, the first one being the Sun. It is 4.2 light-years away from us!
When astronomers measure the distance of stars (from planet Earth) they do not use the kilometre (km) unit but instead they use the light-year unit. One light-year represents the distance that light travels in one year, at the speed of light (300,000 km/s).
The aim of this challenge is to convert the distance between planet Earth and Alpha-Centauri from light-years to km.
To do so we will use a sequencing algorithm based on the following steps:
- Calculate the number of seconds in a year,
- Multiply this number by the speed of light,
- Multiply this number by 4.2 (the distance of Alpha-Centauri from Earth in light-years).
- Output the result/ distance in km
The flowchart of this sequencing algorithm is as follows:
You can now implement this sequencing algorithm using Python code:
Step 2: using Selection
Your next challenge is to let the user choose a star, and based on their choice, you will output the distance between the selected star and planet Earth, in km using the following data:
|Star||Distance from Earth (in light-years)|
|Alpha Centauri||4.24 light-years|
|Barnard’s Star||5.96 light-years|
|Luhman 16||6.59 light-years|
|WISE 0855-0714||7.2 light-years|
|Wolf 359||7.78 light-years|
To do so you will use an if, elif and else statements to implement a selection construct.
Step 3: using Iteration
Finally, using a while loop, you will keep asking the user to choose a star to repeat the above process with the user’s selection, until the user decides to quit the program. Using a while loop is an example of iteration!