Skip to content
Advertisement

How can I display the output as requested?

In Group B of the World Cup, the teams of Iran, Portugal, Spain and Morocco are present. I wrote a program that, after receiving the results of the games, would print the name of the team, the number of wins and losses, the goal difference and their points in one line, respectively. Each team is printed in order of points on one line. (If the score is equal, the number of wins is taken into account. If both the number of wins and the score are equal, they are printed in alphabetical order.)

I wrote the code of this program correctly

my code:

from collections import  OrderedDict

teams = {'Iran':{'wins':0 , 'loses':0 , 'draws':0 , 'goal difference':0 , 'points':0},'Spain':{'wins':0 , 'loses':0 , 'draws':0 , 'goal difference':0 , 'points':0},'Portugal':{'wins':0 , 'loses':0 , 'draws':0 , 'goal difference':0 , 'points':0},'Morocco':{'wins':0 , 'loses':0 , 'draws':0 , 'goal difference':0 , 'points':0}}
results = []

for k in range(6):
    n = input().split('-')
    results.append(n)


if results[0][0] > results[0][1]:
    teams['Iran']['wins'] +=1
    teams['Iran']['points'] +=3
    teams['Iran']['goal difference'] += abs(int(results[0][0])-int(results[0][1]))
    teams['Spain']['loses'] +=1
    teams['Spain']['goal difference'] -= abs(int(results[0][0])-int(results[0][1]))
elif results[0][0] == results[0][1]:
    teams['Iran']['draws'] +=1
    teams['Spain']['draws'] += 1
    teams['Iran']['points'] += 1
    teams['Spain']['points'] += 1
else:
    teams['Spain']['wins'] += 1
    teams['Spain']['points'] += 3
    teams['Spain']['goal difference'] += abs(int(results[0][0])-int(results[0][1]))
    teams['Iran']['loses'] += 1
    teams['Iran']['goal difference'] -= abs(int(results[0][0])-int(results[0][1]))

if results[1][0] > results[1][1]:
    teams['Iran']['wins'] +=1
    teams['Iran']['points'] +=3
    teams['Iran']['goal difference'] += abs(int(results[1][0])-int(results[1][1]))
    teams['Portugal']['loses'] += 1
    teams['Portugal']['goal difference'] -= abs(int(results[1][0])-int(results[1][1]))
elif results[1][0] == results[1][1]:
    teams['Iran']['draws'] +=1
    teams['Portugal']['draws'] += 1
    teams['Iran']['points'] += 1
    teams['Portugal']['points'] += 1
else:
    teams['Portugal']['wins'] += 1
    teams['Portugal']['points'] += 3
    teams['Portugal']['goal difference'] += abs(int(results[1][0])-int(results[1][1]))
    teams['Iran']['loses'] += 1
    teams['Iran']['goal difference'] -= abs(int(results[1][0])-int(results[1][1]))

if results[2][0] > results[2][1]:
    teams['Iran']['wins'] +=1
    teams['Iran']['points'] +=3
    teams['Iran']['goal difference'] += abs(int(results[2][0])-int(results[2][1]))
    teams['Morocco']['loses'] += 1
    teams['Morocco']['goal difference'] -= abs(int(results[2][0])-int(results[2][1]))
elif results[2][0] == results[2][1]:
    teams['Iran']['draws'] +=1
    teams['Morocco']['draws'] += 1
    teams['Iran']['points'] += 1
    teams['Morocco']['points'] += 1
else:
    teams['Morocco']['wins'] += 1
    teams['Morocco']['points'] += 3
    teams['Morocco']['goal difference'] += abs(int(results[2][0])-int(results[2][1]))
    teams['Iran']['loses'] += 1
    teams['Iran']['goal difference'] -= abs(int(results[2][0])-int(results[2][1]))

if results[3][0] > results[3][1]:
    teams['Spain']['wins'] +=1
    teams['Spain']['points'] +=3
    teams['Spain']['goal difference'] += abs(int(results[3][0])-int(results[3][1]))
    teams['Portugal']['loses'] += 1
    teams['Portugal']['goal difference'] -= abs(int(results[3][0])-int(results[3][1]))
elif results[3][0] == results[3][1]:
    teams['Spain']['draws'] +=1
    teams['Portugal']['draws'] += 1
    teams['Spain']['points'] += 1
    teams['Portugal']['points'] += 1
else:
    teams['Portugal']['wins'] += 1
    teams['Portugal']['points'] += 3
    teams['Portugal']['goal difference'] += abs(int(results[3][0])-int(results[3][1]))
    teams['Spain']['loses'] += 1
    teams['Spain']['goal difference'] -= abs(int(results[3][0])-int(results[3][1]))

if results[4][0] > results[4][1]:
    teams['Spain']['wins'] +=1
    teams['Spain']['points'] +=3
    teams['Spain']['goal difference'] += abs(int(results[4][0])-int(results[4][1]))
    teams['Morocco']['loses'] += 1
    teams['Morocco']['goal difference'] -= abs(int(results[4][0])-int(results[4][1]))
elif results[4][0] == results[4][1]:
    teams['Spain']['draws'] +=1
    teams['Morocco']['draws'] += 1
    teams['Spain']['points'] += 1
    teams['Morocco']['points'] += 1
else:
    teams['Morocco']['wins'] += 1
    teams['Morocco']['points'] += 3
    teams['Morocco']['goal difference'] += abs(int(results[4][0])-int(results[4][1]))
    teams['Spain']['loses'] += 1
    teams['Spain']['goal difference'] -= abs(int(results[4][0])-int(results[4][1]))



if results[5][0] > results[5][1]:
    teams['Portugal']['wins'] +=1
    teams['Portugal']['points'] +=3
    teams['Portugal']['goal difference'] += abs(int(results[5][0])-int(results[5][1]))
    teams['Morocco']['loses'] += 1
    teams['Morocco']['goal difference'] -= abs(int(results[5][0])-int(results[5][1]))
elif results[5][0] == results[5][1]:
    teams['Portugal']['draws'] +=1
    teams['Morocco']['draws'] += 1
    teams['Portugal']['points'] += 1
    teams['Morocco']['points'] += 1
else:
    teams['Morocco']['wins'] += 1
    teams['Morocco']['points'] += 3
    teams['Morocco']['goal difference'] += abs(int(results[5][0])-int(results[5][1]))
    teams['Portugal']['loses'] += 1
    teams['Portugal']['goal difference'] -= abs(int(results[5][0])-int(results[5][1]))

so = OrderedDict(sorted(teams.items(),key=lambda x:(-x[1]['points'])))

for k,v in so.items():
    b = str(v).replace('{',"").replace('}',"")
    print(k,v)


The input I give to the program:

2-2
2-1
1-2
2-2
3-1
2-1

The correct output format is as follows:

Spain  wins:1 , loses:0 , draws:2 , goal difference:2 , points:5
Iran  wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Portugal  wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Morocco  wins:1 , loses:2 , draws:0 , goal difference:-2 , points:3

But the output format of my code is as follows:

Spain 'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5
Iran 'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4
Portugal 'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4
Morocco 'wins': 1, 'loses': 2, 'draws': 0, 'goal difference': -2, 'points': 3

Please help me display the output in the correct format.

Advertisement

Answer

Does this work for you?

for k, v in so.items():
    row = [(sub_key, v[sub_key]) for sub_key in ('wins', 'loses', 'draws', 'goal difference', 'points')]
    row_s = " , ".join([f"{k}:{v}" for k, v in row])
    print(f"{k} {row_s}")

Output is

Spain wins:1 , loses:0 , draws:2 , goal difference:2 , points:5
Iran wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Portugal wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Morocco wins:1 , loses:2 , draws:0 , goal difference:-2 , points:3

However, I strongly suggest you consider reading some introductory tutorials about programming in Python. Take special care about for-loops. Your code is very repetitive, meaning it contains large blocks that look almost the same. This is usually an indication for a missed abstraction in the program.

I assume your code of about 120 lines could be written with about 50.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement