1

I am using this dictionary, appending another value to each planet with mark_planets(), and then printing out a table with defined specifications.

############### Skip this, this is just to have the formula run right
sol = {"Uranus": [2750, 3000, 2880], "Mercury": [46, 70, 57], "Earth": [147, 152, 150], "Venus": [107, 109, 108],
       "Mars": [205, 249, 228], "Saturn": [1350, 1510, 1430], "Jupiter": [741, 817, 779],
       "Pluto": [4440, 7380, 5910], "Neptune": [4450, 4550, 4500]}

status = [True, True, True, True, True, True, True, False, True]

def mark_planets(planets, stat):
    idx = 0
    for planet in planets:
        if stat[idx]:
            planets[planet].append("Planet")
            idx += 1
        else:
            planets[planet].append("Dwarf")
            idx += 1
################Below is the issue
def display_planets(sol):
    print("{:>10} {:>10} {:>15} {:>15} {:>15}".format("planet", "status", "nearest", "furthest", "average"))
    print("{:-^69s}".format("-"))
    for planet in sol:
        print("{:>10} {:>10} {:>15} {:>15} {:>15}".format(planet, sol[planet][3], sol[planet][0], sol[planet][1],sol[planet][2]))

So as shown, I have correct spacing for the output, but i need to format value[0],[1],[2] with commas between the numbers. I am not sure how to format the commas to the string and keep the correct spacing for it to print properly.

Below is the output I currently have.

    planet     status         nearest        furthest         average
---------------------------------------------------------------------
    Uranus     Planet      2750000000      3000000000      2880000000
   Mercury     Planet        46000000        70000000        57000000
     Earth     Planet       147000000       152000000       150000000
     Venus     Planet       107000000       109000000       108000000
      Mars     Planet       205000000       249000000       228000000
    Saturn     Planet      1350000000      1510000000      1430000000
   Jupiter     Planet       741000000       817000000       779000000
     Pluto      Dwarf      4440000000      7380000000      5910000000
   Neptune     Planet      4450000000      4550000000      4500000000``

Just adding the comma's to the numbers is what I am struggling with. Thanks for the help.

Jason Mack
  • 13
  • 2
  • 1
    Because of the big numbers, I would suggest using scientific notation, e.g. `'{:.3e}'.format(num)` would format `2750000000` as `'2.750e+09'` – Noah May Apr 26 '20 at 20:15
  • Hey Noah, Thanks for the tip, but specifications outlined to have it in this format with commas. – Jason Mack Apr 26 '20 at 20:17

1 Answers1

3

The answer is originated from Ian Schneider's answer

You can pass a , to your integer format specification at the end so it reads like:

some_value = 12345678
"{:>15,}".format(some_value)
Masked Man
  • 2,176
  • 2
  • 22
  • 41