I have the following item lists:
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
sunDist = [57.9, 108.2, 149.6, 227.9, 778.3, 1427, 2870, 4497, 5900]
revolut = [88, 224.7, 365.2, 687, 4328, 10752, 651160, 1020707, 1338090]
satsNum = [0, 0, 1, 2, 63, 56, 27, 13, 3]
dlabels = ['Planet', 'Distance from the Sun', 'Duration of the year', 'Number of satellites']
Exercise 1. Print a list of planets sorted according to their number of satellites (from low to high). (Provide the solution writing your own sorting method (e.g. Bubble Sort))
My solution:
satsforplanet = list(zip(satsNum, planets))
def bubble(list_a):
indexing_length = len(list_a) - 1
sorted = False
while not sorted:
sorted = True
for i in range(0, indexing_length):
if list_a[i][0] > list_a[i+1][0]:
sorted = False
list_a[i], list_a[i+1] = list_a[i+1], list_a[i]
sortedlist = []
for element in list_a:
sortedlist.append(element[1])
return sortedlist
Exercise 2. Write a function planetData(pName) that takes the name of a planet and returns a dictionary with all the data for that planet, where the keys are the labels in dlabels and the values the relevant data.
My solution:
def planetData(pName):
zipped = list(zip(planets, sunDist, revolut, satsNum))
dic = {}
for i in range(len(planets)):
nameplanet = planets[i]
dic[nameplanet] = {}
j = 0
for key in dlabels:
dic[nameplanet][key] = zipped[i][j]
j += 1
if pName in dic:
return dic[pName]
else:
return 'Write the right name of the planet'
Exercise 3 Define a class Planet that stores all the information about a planet. Adapt the code you wrote for Exercises 1 and 2 to this new type. Further, define a method of the Planet class that takes as input a number of days and returns to how many years they correspond for the specific planet.
Does anyone have any idea how to solve the third exercise? I have no idea where to start.
I know that for each planet I have to store all information relating to the planet and be able to recall all the information I need, but I'm not sure how the exercise would like me to do it.
I thought I had to define the Planet
class starting from the names of the planets, giving each planet the attributes contained in the dlabels
list.
I think the result should be, for example, that if I write
print(Pluto)
, the output should be a dictionary like the output in the second exercise:
{'Planet': 'Pluto',
'Distance from the Sun': 5900,
'Duration of the year': 1338090,
'Number of satellites': 3}
Instead, if I write Pluto.name
, the output should be 'Pluto'
, if I write Pluto.satellites
the output should be 0
and so on.
The problem is that I have no idea how to iterate this procedure for all the planets, anyone can help me, please?