Python Forum
Display 8 elements per line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display 8 elements per line
#1
I have a function called display(dict) that takes a dictionary as a parameter and displays all the elements of the dictionary. It should only display 8 elements per line. In this program I read a file filled with teams that won the baseball world series. The key in the dictionary is the name of the team and the value is a set filled with the years each team won. But I can't get my code to print out 8 values per line. Can you please look at my code and give me some tips?
 def display(winning_dict):
    count = 1
    for key, value in winning_dict.items():
        print(key, ':')
        print(value)
        count += 1
        if count == 8:
            print(value)
This is an example of what my code display:
Output:
New York Yankees : {1923, 1927, 1928, 1932, 1936, 1937, 1938, 1939, 1941, 1943, 1947, 1949, 1950, 1951, 1952, 1953, 1956, 1958, 1961, 1962, 1977, 1978, 1996, 1998, 1999, 2000} Washington Senators : {1924} none : {1904, 1994}
You will notice in my output that it display the years no team won the world series. I am not supposed to display this part of the dictionary but I am not allowed to just delete it. How can I avoid printing this part out?
Reply
#2
How do you think this is supposed to work?
   count = 1
   for key, value in winning_dict.items():
       print(key, ':')  ## Prints the team name
       print(value)     ## Prints the years
       count += 1
       if count == 8:
           print(value)  ## Huh?
I look at this and what I see is that after printing out results for 8 teams you print value again. Something like this
Output:
Team 1: [1, 2 3] Team 2: [1, 2, 3] ... Team 8: [1, 2, 3] [1, 2, 3]
If you want to split up the years into groups of 8, you need to do something special when printing "value", not print value twice.

Forget about your baseball program and dictionaries and start a new program that has a list with 21 elements that you want to print out 8 at a time.
value= list(range(1939, 1962)
## How do I print out only the first 8 years in list?
## How do I repeat printing the next 8 years on a new line until I run out of years[/python]
When you get this working fold it into your baseball program. It is easier to solve one problem at a time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seven Segment Display - QUERY on Multi-line side-by-side printing output ajayachander 3 6,009 Mar-13-2020, 07:02 AM
Last Post: ajayachander

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020