如何从给定的文本创建一个字典,并使用for循环将其输出

2024-04-29 16:09:48 发布

您现在位置:Python中文网/ 问答频道 /正文

根据以下关于我们太阳系行星的信息,创建一个字典。(我将输出放在下面,抱歉,我是新手,可能没有意义地发布全文)

水银

半径-2439.7公里

Distance from the sun - 58 million km

Moons - none

Atmosphere? True

Gas planet? False

维纳斯

^{pr2}$

地球

Radius - 6,371.0 km

Distance from the sun - 150 million km

Moons - Moon

Atmosphere? True

Gas planet? False

火星

Radius - 3,396.2 km

Distance from the sun - 207 million km

Moons - Phobos and Deimos

Atmosphere? True

Gas planet? False

朱庇特

Radius - 69,911 km

Distance from the sun - 483 million km

Moons - Io, Ganymede, Callisto, Europa, Adrastea

Atmosphere? True

Gas planet? True

土星

Radius - 60,268 km

Distance from the sun - 1,400 million km

Moons - Pan, Prometheus, Titan, Phoebe, Rhea

Atmosphere? True

Gas planet? True

天王星

Radius - 25,559 km

Distance from the sun - 3,000 million km

Moons - Miranda, Ariel, Umbriel, Titania, Oberon

Atmosphere? True

Gas planet? True

海王星

Radius - 24,764 km

Distance from the sun - 4,500 million km

Moons - Triton, Nereid, Proteus, Naiad, Thalassa

Atmosphere? True

Gas planet? True

程序将上述信息存储在一个字典中,然后使用for循环打印字典的内容。

样本输出:

水银

与太阳的距离:58

半径:2439.7

气体行星?:错误

气氛?:正确

月亮:【】

朱庇特

与太阳的距离:483

半径:69911

气体行星?:正确

气氛?:正确

卫星:['Io'、'Ganymede'、'Callisto'、'Europa'、'Adrastea']

天王星

距太阳3000

半径:25559

气体行星?:正确

气氛?:正确

卫星:['Miranda'、'Ariel'、'Umbriel'、'Titania'、'Oberon']

火星

与太阳的距离:207

半径:3396.2

气体行星?:错误

气氛?:正确

卫星:['Phobos','Deimos']

地球

与太阳的距离:150

半径:6371.0

气体行星?:错误

气氛?:正确

月亮:['Moon']

维纳斯

与太阳的距离:108

半径:6051.8

气体行星?:错误

气氛?:正确

月亮:【】

土星

距太阳1400

半径:60268

气体行星?:正确

气氛?:正确

卫星:['Pan','普罗米修斯','泰坦','菲比','瑞亚']

海王星

距太阳4500

半径:24764

气体行星?:正确

气氛?:正确

卫星:['Triton'、'Nereid'、'Proteus'、'Naiad'、'Thalassa']

所以我想知道如何将输入输入输入到字典中,然后按上面给出的方式输出,我尝试将其保存到字典中,但我认为我做得不对,因为我不断地出现语法错误,我将非常感谢任何帮助!!!,我已经盯着这个看了几个小时了。


Tags: thefromtrue半径行星distancesungas
1条回答
网友
1楼 · 发布于 2024-04-29 16:09:48

好的,除非您被明确告知要使用Dictionary,否则存储此数据的数据结构是错误的。字典是设计用来存储数据的键对书名:ISBN)在

附录:While”半径:2439.7“等等。。。作为键值对的例子,使字典在这种情况下不合适的原因是所有的对都与一个“元”定义相关联(在这种情况下,所有的信息对都是单个行星的一部分)。如果有道理的话。在

由于每个行星都有许多与之相关的数据字段,所以您需要定义一个Planet类,将所有数据封装在一个对象中。在

示例:

class Planet:  
    def __init__(self, name, radius, distFromSun, moons, atmosphere, isGasPlanet):
        """Planet Constructor"""
        self.name = name
        self.radius = radius
        self.distFromSun = distFromSun
        self.moons = moons
        self.atmosphere = atmosphere
        self.isGasPlanet = isGasPlanet

    def __str__(self):
        """Overwrites the string output of the class (what gets called by print())""" 

        return str.format("{0}\n"
                          "Distance from the sun: {1}\n"
                          "Radius: {2}\n"
                          "Gas Planet: {3}\n"
                          "Atmosphere: {4}\n"
                          "Moons: {5}\n",
                          self.name,
                          self.distFromSun,
                          self.radius,
                          self.isGasPlanet,
                          self.atmosphere,
                          self.moons)

这可以让你定义任何你喜欢的行星,并轻松打印出来。在

^{pr2}$

会给你的。在

Mercury
Distance from the sun: 58
Radius: 2439.7
Gas Planet: False
Atmosphere: True
Moons: []

此外,类是可扩展的,允许您在它们的基础上进行构建。例如,您可以实现一个AddMoon()方法,将一个新月附加到行星对象内部的“月亮数组”中。在

相关问题 更多 >