CBSE Class 11 Computer Science Question 22 of 24

Dictionaries — Question 4

Back to all questions
4
Question

Question 4

Repeatedly ask the user to enter a team name and how many games the team has won and how many they lost. Store this information in a dictionary where the keys are the team names and the values are lists of the form [wins, losses].

(a) Using the dictionary created above, allow the user to enter a team name and print out the team's winning percentage.

(b) Using the dictionary, create a list whose entries are the number of wins of each team.

(c) Using the dictionary, create a list of all those teams that have winning records.

Solution
d = {}
ans = "y"
while ans == "y" or ans == "Y" :
    name = input("Enter Team name: ")
    w = int(input("Enter number of wins: "))
    l = int(input("Enter number of losses: "))
    d[name] = [w, l]
    ans = input("Do you want to enter more team names? (y/n): ")

team = input("Enter team name for winning percentage: ")
if team not in d:
    print("Team not found", team)
else:
    wp = d[team][0] / sum(d[team]) * 100
    print("Winning percentage of", team, "is", wp)
 
w_team = []   
for i in d.values():
    w_team.append(i[0]) 

print("Number of wins of each team", w_team)

w_rec = []
for i in d:
    if d[i][0] > 0:
        w_rec.append(i)

print("Teams having winning records are:", w_rec)
Output
Enter Team name: masters
Enter number of wins: 9
Enter number of losses: 1
Do you want to enter more team names? (y/n): y
Enter Team name: musketeers
Enter number of wins: 6
Enter number of losses: 4
Do you want to enter more team names? (y/n): y
Enter Team name: challengers
Enter number of wins: 0
Enter number of losses: 10
Do you want to enter more team names? (y/n): n
Enter team name for winning percentage: musketeers
Winning percentage of musketeers is 60.0
Number of wins of each team [9, 6, 0]
Teams having winning records are: ['masters', 'musketeers']
Answer

d
=
{}
ans
=
"y"
while
ans
==
"y"
or
ans
==
"Y"
:
name
=
input
(
"Enter Team name: "
)
w
=
int
(
input
(
"Enter number of wins: "
))
l
=
int
(
input
(
"Enter number of losses: "
))
d
[
name
]
=
[
w
,
l
]
ans
=
input
(
"Do you want to enter more team names? (y/n): "
)
team
=
input
(
"Enter team name for winning percentage: "
)
if
team
not
in
d
:
print
(
"Team not found"
,
team
)
else
:
wp
=
d
[
team
][
0
]
/
sum
(
d
[
team
])
*
100
print
(
"Winning percentage of"
,
team
,
"is"
,
wp
)
w_team
=
[]
for
i
in
d
.
values
():
w_team
.
append
(
i
[
0
])
print
(
"Number of wins of each team"
,
w_team
)
w_rec
=
[]
for
i
in
d
:
if
d
[
i
][
0
]
>
0
:
w_rec
.
append
(
i
)
print
(
"Teams having winning records are:"
,
w_rec
)
Output
Enter Team name: masters
Enter number of wins: 9
Enter number of losses: 1
Do you want to enter more team names? (y/n): y
Enter Team name: musketeers
Enter number of wins: 6
Enter number of losses: 4
Do you want to enter more team names? (y/n): y
Enter Team name: challengers
Enter number of wins: 0
Enter number of losses: 10
Do you want to enter more team names? (y/n): n
Enter team name for winning percentage: musketeers
Winning percentage of musketeers is 60.0
Number of wins of each team [9, 6, 0]
Teams having winning records are: ['masters', 'musketeers']