Home > Software engineering >  Loop is printing too many times
Loop is printing too many times

Time:10-05

enter image description here

def main():
    lightning = dict()
    with open("lightning.txt","r") as f:
        for name,goals,assists in zip(f,f,f):
            lightning[name.strip()] = [int(goals),int(assists)]
        
    f.close()

    title_Player = 'Player'
    title_Goals = 'Goals'
    title_Assists = 'Assists'
    title_Total = 'Total'

    print('{:<10}{:^6}{:^8}{:^6}'.format(title_Player, title_Goals, title_Assists, title_Total))
    highest_points = 0

    for k in lightning:
        goals = lightning[k][0]
        assist = lightning[k][1]
        total = goals   assist
        print('{:<8}'.format(k), end = ' ')
    
        for v in lightning[k]:
            print('{:^6}{:^8}'.format(goals, assist), end = ' ')
        print('{:^6}'.format(total))


        if total > highest_points:
            highest_points = total
            top_scorer = k
    print(top_scorer, 'is the top scorer with', highest_points, 'points')

if __name__ == '__main__':
    main()

Here is my code, the goals and assist are displaying twice in the output, I've tried tweaking around with parts of the for loops but haven't had any success. All of the column formatting is correct though.

CodePudding user response:

Right here:

for v in lightning[k]:
        print('{:^6}{:^8}'.format(goals, assist), end = ' ')

Since you're iterating through lightning[k] (which has two values, the goals and the assists), you're running that print statement (which prints out both the goals and the assists) twice. Instead, to fit with the header of your table, you should probably just change this:

print('{:<8}'.format(k), end = ' ')

for v in lightning[k]:
    print('{:^6}{:^8}'.format(goals, assist), end = ' ')
print('{:^6}'.format(total))

to this:

print('{:<10}{:^6}{:^8}{:^6}'.format(k, goals, assist, total))

which would print the name, the goals, the assists, and the total, all at once, in columns that are the same size as the columns in your header (10, 6, 8, 6).

CodePudding user response:

You need to nix this for loop here:

        for v in lightning[k]: <<<<<<<<<<<<<
            print('{:^6}{:^8}'.format(goals, assist), end = ' ')

Instead, you can just directly use the print statement.

Breaking down your code to help explain why:

    lightning = dict()
    with open("lightning.txt","r") as f:
        for name,goals,assists in zip(f,f,f):
            lightning[name.strip()] = [int(goals),int(assists)]

Your dictionary is a mapping of <name : <goals, assists>>

    for k in lightning:
        goals = lightning[k][0]
        assist = lightning[k][1]
        total = goals   assist
        print('{:<8}'.format(k), end = ' ')

Here you iterate over your dictionary, this means you're executing this loop once per player, and printing out their name. This is good.

        for v in lightning[k]:
            print('{:^6}{:^8}'.format(goals, assist), end = ' ')
        print('{:^6}'.format(total))

Here you iterate over the value of your dictionary entry, which is the <goals, assists> pair from earlier. So you execute this print statement twice!

        print('{:^6}'.format(total))

And here we're back to printing once, and so you are seeing the total get output only once (as expected).

  • Related