In: Computer Science
IN PYTHON
create a python program that accepts input from the user in the following sequence:
1. Planet Name
2. Planet Gravitational Force(g)
for data, use the planets of our solar system. The data input is to be written in 2 separate lists, the names of which are:
1. planetName
2. planet GravitationalForce(g)
A third list is required that will store the weight of a person with mass of 100kg, the formula of which is: W=mg(where m is mass of 100kg, and g is gravitational force of the planet)
Name this list:
3. weightOnPlanet
your are required to use loops in inputting the required data. Data inputting is to continue until the sentinel value of QUIT is entered for he planet name.
print out the results on screen in readable format
i.e. (Planet Name: Earth, Gravitational Force: xxxx, Weight: xxxx
Pluto is not considered by many not to be a planet of our solar system anymore.
Modify the same program to remove Pluto from all 3 lists in the program. Reprint the lists again, in the same format as the previous print.
A hypothetical object called Planet Nice is could exist in the outermost regions of our solar system. It is conjectured it maybe about 10 times the mass of planet Earth and let's suppose the 5 times the gravitational for of earth.
Modify your program to insert this hypothetical object in the 3rd position of your lists, including the computation of weight of a 100kg person.
REPRINT THE LISTS AGAIN IN THE SAME FORMAT AS THE PREVIOUS 2 PRINTS
Solution:
The program is implemented as shown below. The sentinel is set for Quit.
When the user enters 0 in the planetName the inputting stops.
Code:
Quit=1 #setting sentinel for Quit
planetName=[]
planetGravitationalForce=[]
weightOnPlanet=[]
while(Quit!=0):
planetName.append(input("Enter planet name and enter 0 if you want
to stop inputting")) #Read input of planet name
if(planetName[-1]=='0'):
Quit=0 #If quit is requested, set sentinel and quit
continue
planetGravitationalForce.append(float(input("Enter gravitational
force"))) #Read input of gravity
for i in planetGravitationalForce:
weightOnPlanet.append(100*i) #compute weight using w=mg and append
it.
planetName.remove('0')
for i in range(len(planetName)): #print the data
print("Planet Name=", planetName[i])
print("Gravitational Force=", planetGravitationalForce[i])
print("Weight=", weightOnPlanet[i])
#removing Pluto
print("After removing Pluto")
index=planetName.index("Pluto")
planetName.pop(index) #Removing the pluto from plane name
list
planetGravitationalForce.pop(index) #Removing pluto gravity from
list
weightOnPlanet.pop(index) #Removing pluto weight from its
list
for i in range(len(planetName)):
print("Planet Name=", planetName[i]) #Print the data after removing
pluto
print("Gravitational Force=", planetGravitationalForce[i])
print("Weight=", weightOnPlanet[i])
#Inserting Nice
print("After inserting Nice")
planetName.insert(2,"Nice") #Insert Nice at index 2 i.e., at
position 3 in list.
index=planetName.index("Earth")-1
planetGravitationalForce.insert(2,5*planetGravitationalForce[index])
#insert gravity of Nice in its list
weightOnPlanet.insert(2,10*weightOnPlanet[index]) #insert weight of
Nice in its list.
for i in range(len(planetName)):
print("Planet Name=", planetName[i]) #print the data after
insertion of Nice
print("Gravitational Force=", planetGravitationalForce[i])
print("Weight=", weightOnPlanet[i])
Screenshot:
The screenshot of the program is attached below along with the output generated.
Please follow the below screenshot for proper indentation.
See that the gravitational values for planets are nor correct. They are some random values entered in the list.