0

psetHere is my code so far:

MEDALS = 3
COUNTRIES = 8
# Create a list of country names.
countries = [ "Canada", "Italy", "Germany", "Japan",
            "Kazakhstan", "China",
            "South Korea", "United States" ]
# Create a table of medal counts.
counts = [
            [ 0, 3, 0 ],
            [ 0, 0, 1 ],
            [ 0, 0, 1 ],
            [ 1, 0, 0 ],
            [ 0, 0, 1 ],
            [ 3, 1, 1 ],
            [ 0, 1, 0 ],
            [ 1, 0, 1 ]
        ]

print('       Country  Gold  Silver  Bronze  Total')
for v in countries:
    country = v
    print("{:>13}".format(country))
for row in range(COUNTRIES):
    for col in range(MEDALS):
        print(counts[row][col],end = ' ')
    print('')

Supposed to solve this without using any imported modules. How can I do this and please explain it to me? thank you so very much.

I can get the numbers to print under the table but not in the columns of the table like it is supposed to be.

5
  • In what way it does not work? What's the error message? Commented Nov 30, 2023 at 12:38
  • Well it works but the numbers of medals just prints under the table that ive made so far Commented Nov 30, 2023 at 12:40
  • I included the problem for this in the link 'pset' Commented Nov 30, 2023 at 12:42
  • Check out what zip(countries, counts) gives you back.
    – JonSG
    Commented Nov 30, 2023 at 13:18
  • Where do I add the zip function to try this? Commented Nov 30, 2023 at 13:23

3 Answers 3

0

So the main trick here is to join the corresponding countries with their medal counts and iterate over that as a single thing.

for country_name, medal_counts in zip(countries, counts):
    gold, silver, bronze = medal_counts
    total = sum(medal_counts)
    print(country_name, gold, silver, bronze, total)

Note that there are lots of ways to get the same results as zip().

For example:

for index in range(len(countries)):
    country_name = countries[index]
    medal_counts = counts[index]

    gold, silver, bronze = medal_counts
    total = sum(medal_counts)
    print(country_name, gold, silver, bronze, total)

zip() just does it for you.

Once you get that far, you more or less have the full answer and just need to use a nice format template. To polish it off, lets use a format template like the one you started to use.

# Create a list of country names.
countries = [
    "Canada",
    "Italy",
    "Germany",
    "Japan",
    "Kazakhstan",
    "China",
    "South Korea",
    "United States"
]

# Create a table of medal counts.
counts = [
    [ 0, 3, 0 ],
    [ 0, 0, 1 ],
    [ 0, 0, 1 ],
    [ 1, 0, 0 ],
    [ 0, 0, 1 ],
    [ 3, 1, 1 ],
    [ 0, 1, 0 ],
    [ 1, 0, 1 ]
]

## -----------------
## Now we just need to make it look nice
## -----------------
template = "{country_name:>15} {gold:>8} {silver:>8} {bronze:>8} {total:>8} "
## -----------------

print(template.format(country_name="", gold="Gold", silver="Silver", bronze="Bronze", total="Total"))
for country_name, medal_counts in zip(countries, counts):
    gold, silver, bronze = medal_counts
    total = sum(medal_counts)
    print(template.format(country_name=country_name, gold=gold, silver=silver, bronze=bronze, total=total))

That will give you:

                    Gold   Silver   Bronze    Total
         Canada        0        3        0        3
          Italy        0        0        1        1
        Germany        0        0        1        1
          Japan        1        0        0        1
     Kazakhstan        0        0        1        1
          China        3        1        1        5
    South Korea        0        1        0        1
  United States        1        0        1        2
2
  • 1
    wow thank you so much, Im familiar with zip but I didnt think to use it here. Also, what is the syntax you used to format the skew of the countries? like what is it called when you type '{country_name:>15}'? Commented Nov 30, 2023 at 13:43
  • That is a format string spec. check out Format String Syntax
    – JonSG
    Commented Nov 30, 2023 at 14:52
0

Your code now says: print all countries for v in countries: then print all medals for row in range(COUNTRIES), to print table you need to nest your second loop within first loop.

0
0

So it turns out that the indentation of your second for loop is incorrect.

Code

MEDALS = 3
COUNTRIES = 8
# Create a list of country names.
countries = ["Canada", "Italy", "Germany", "Japan",
             "Kazakhstan", "China",
             "South Korea", "United States"]
# Create a table of medal counts.
counts = [
    [0, 3, 0],
    [0, 0, 1],
    [0, 0, 1],
    [1, 0, 0],
    [0, 0, 1],
    [3, 1, 1],
    [0, 1, 0],
    [1, 0, 1]
]

print('       Country  Gold  Silver  Bronze  Total')
for row in range(COUNTRIES):
    country = countries[row]
    print("{:>13}".format(country), end=' ')
    for col in range(MEDALS):
        print("{:<7}".format(counts[row][col]), end=' ')
    total = sum(counts[row])
    print("{:<6}".format(total))

I have fixed your code, it works fine now.

Output

Output

Hopefully this helps, good luck :)

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.