1个 python dictionary 问题求助! 15
问题三:Giventhefollowinginformationabouttheplanetsinoursolarsystem,createasingledictiona...
问题三: Given the following information about the planets in our solar system, create a single dictionary. While some planets have over 60 moons, we have kept it simple and only listed a max of five per planet.
Mercury
Radius - 2,439.7 km
Distance from the sun - 58 million km
Moons - none
Atmosphere? True
Gas planet? False
Venus
Radius - 6,051.8 km
Distance from the sun - 108 million km
Moons - none
Atmosphere? True
Gas planet? False
Earth
Radius - 6,371.0 km
Distance from the sun - 150 million km
Moons - Moon
Atmosphere? True
Gas planet? False
Mars
Radius - 3,396.2 km
Distance from the sun - 207 million km
Moons - Phobos and Deimos
Atmosphere? True
Gas planet? False
Jupiter
Radius - 69,911 km
Distance from the sun - 483 million km
Moons - Io, Ganymede, Callisto, Europa, Adrastea
Atmosphere? True
Gas planet? True
Saturn
Radius - 60,268 km
Distance from the sun - 1,400 million km
Moons - Pan, Prometheus, Titan, Phoebe, Rhea
Atmosphere? True
Gas planet? True
Uranus
Radius - 25,559 km
Distance from the sun - 3,000 million km
Moons - Miranda, Ariel, Umbriel, Titania, Oberon
Atmosphere? True
Gas planet? True
Neptune
Radius - 24,764 km
Distance from the sun - 4,500 million km
Moons - Triton, Nereid, Proteus, Naiad, Thalassa
Atmosphere? True
Gas planet? True
The program stores the above information in a single dictionary and then prints the contents of the dictionary using a for loop.
Sample Output:
Mercury
Distance from the sun : 58
Radius : 2439.7
Gas planet? : False
Atmosphere? : True
Moons : [ ]
Jupiter
Distance from the sun : 483
Radius : 69911
Gas planet? : True
Atmosphere? : True
Moons : ['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea']
Uranus
Distance from the sun : 3000
Radius : 25559
Gas planet? : True
Atmosphere? : True
Moons : ['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon']
Mars
Distance from the sun : 207
Radius : 3396.2
Gas planet? : False
Atmosphere? : True
Moons : ['Phobos', 'Deimos']
Earth
Distance from the sun : 150
Radius : 6371.0
Gas planet? : False
Atmosphere? : True
Moons : ['Moon']
Venus
Distance from the sun : 108
Radius : 6051.8
Gas planet? : False
Atmosphere? : True
Moons : [ ]
Saturn
Distance from the sun : 1400
Radius : 60268
Gas planet? : True
Atmosphere? : True
Moons : ['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea']
Neptune
Distance from the sun : 4500
Radius : 24764
Gas planet? : True
Atmosphere? : True
Moons : ['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa'] 展开
Mercury
Radius - 2,439.7 km
Distance from the sun - 58 million km
Moons - none
Atmosphere? True
Gas planet? False
Venus
Radius - 6,051.8 km
Distance from the sun - 108 million km
Moons - none
Atmosphere? True
Gas planet? False
Earth
Radius - 6,371.0 km
Distance from the sun - 150 million km
Moons - Moon
Atmosphere? True
Gas planet? False
Mars
Radius - 3,396.2 km
Distance from the sun - 207 million km
Moons - Phobos and Deimos
Atmosphere? True
Gas planet? False
Jupiter
Radius - 69,911 km
Distance from the sun - 483 million km
Moons - Io, Ganymede, Callisto, Europa, Adrastea
Atmosphere? True
Gas planet? True
Saturn
Radius - 60,268 km
Distance from the sun - 1,400 million km
Moons - Pan, Prometheus, Titan, Phoebe, Rhea
Atmosphere? True
Gas planet? True
Uranus
Radius - 25,559 km
Distance from the sun - 3,000 million km
Moons - Miranda, Ariel, Umbriel, Titania, Oberon
Atmosphere? True
Gas planet? True
Neptune
Radius - 24,764 km
Distance from the sun - 4,500 million km
Moons - Triton, Nereid, Proteus, Naiad, Thalassa
Atmosphere? True
Gas planet? True
The program stores the above information in a single dictionary and then prints the contents of the dictionary using a for loop.
Sample Output:
Mercury
Distance from the sun : 58
Radius : 2439.7
Gas planet? : False
Atmosphere? : True
Moons : [ ]
Jupiter
Distance from the sun : 483
Radius : 69911
Gas planet? : True
Atmosphere? : True
Moons : ['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea']
Uranus
Distance from the sun : 3000
Radius : 25559
Gas planet? : True
Atmosphere? : True
Moons : ['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon']
Mars
Distance from the sun : 207
Radius : 3396.2
Gas planet? : False
Atmosphere? : True
Moons : ['Phobos', 'Deimos']
Earth
Distance from the sun : 150
Radius : 6371.0
Gas planet? : False
Atmosphere? : True
Moons : ['Moon']
Venus
Distance from the sun : 108
Radius : 6051.8
Gas planet? : False
Atmosphere? : True
Moons : [ ]
Saturn
Distance from the sun : 1400
Radius : 60268
Gas planet? : True
Atmosphere? : True
Moons : ['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea']
Neptune
Distance from the sun : 4500
Radius : 24764
Gas planet? : True
Atmosphere? : True
Moons : ['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa'] 展开
2个回答
展开全部
自己建个class object先,名字叫planet,然后定义的时候把信息作为参数填进去然后存起来。然后再建立个dictonary,然后把object作为value, planet name作为key存进去。print的时候循环扫一遍字典就可以了。 提供了下实现代码,数据只输入了3个,可以作为例子参考:Earth, Jupiter, Uranus, 其他的你自行添加一下吧.
planet里的参数为:Radius, DistanceFromTheSun, isGasPlanet, hasAtmosphere, Moon
(moon如果没有的话可以输入 [] 或者干脆不输入)
class planet:
def __init__(self, radius, distance, hasAtmosphere, isGasplanet,moon=[]):
self.r = float(radius)
self.d = distance
self.a = hasAtmosphere
self.g = isGasplanet
self.mList = moon
def getRadius(self):
return self.r
def getDistance(self):
return self.d
def hasAtmosphere(self):
return self.a
def isGasplanet(self):
return self.g
def getMoon(self):
return self.mList
solarSystem = dict()
solarSystem['Earth'] = planet(6371.0, 150, True, False, ['Moon'])
solarSystem['Jupiter'] = planet(69911, 483, True, True, \
['Pan','Prometheus','Titan','Phoebe','Rhea'])
solarSystem['Uranus'] = planet(25559, 3000, True, True, \
['Miranda','Ariel','Umbriel','Titania','Oberon'])
'''
print result from solar system dictornary
'''
for name, planet in solarSystem.iteritems():
print name
print "Distance from the sun: ", planet.getDistance()
print "Radius: ",planet.getRadius()
print "Gas Planet? :",planet.isGasplanet()
print "Atmosphere? :",planet.hasAtmosphere()
print "Moons: ",planet.getMoon()
planet里的参数为:Radius, DistanceFromTheSun, isGasPlanet, hasAtmosphere, Moon
(moon如果没有的话可以输入 [] 或者干脆不输入)
class planet:
def __init__(self, radius, distance, hasAtmosphere, isGasplanet,moon=[]):
self.r = float(radius)
self.d = distance
self.a = hasAtmosphere
self.g = isGasplanet
self.mList = moon
def getRadius(self):
return self.r
def getDistance(self):
return self.d
def hasAtmosphere(self):
return self.a
def isGasplanet(self):
return self.g
def getMoon(self):
return self.mList
solarSystem = dict()
solarSystem['Earth'] = planet(6371.0, 150, True, False, ['Moon'])
solarSystem['Jupiter'] = planet(69911, 483, True, True, \
['Pan','Prometheus','Titan','Phoebe','Rhea'])
solarSystem['Uranus'] = planet(25559, 3000, True, True, \
['Miranda','Ariel','Umbriel','Titania','Oberon'])
'''
print result from solar system dictornary
'''
for name, planet in solarSystem.iteritems():
print name
print "Distance from the sun: ", planet.getDistance()
print "Radius: ",planet.getRadius()
print "Gas Planet? :",planet.isGasplanet()
print "Atmosphere? :",planet.hasAtmosphere()
print "Moons: ",planet.getMoon()
2013-04-02
展开全部
作业题?循环读入不就好了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询