I want to assign multiple values to Teams, lose and won in dictionary for every team entered by the user.
JavaScript
x
22
22
1
def cricket_teams():
2
no_of_teams=int(input("Please enter the number of the teams: "))
3
4
main_dic={}
5
for i in range(0,no_of_teams):
6
7
main_dic["Team"]=[]
8
main_dic["won"]=[]
9
main_dic["lose"]=[]
10
name=str(input("Please enter the name of team: "))
11
won_=int(input("How many time this team won a match: "))
12
lose_=int(input("How many times this lose a match: "))
13
main_dic["Name"].append(name)
14
main_dic["won"].append(won_)
15
main_dic["lose"].append(lose_)
16
17
18
print(main_dic)
19
20
21
cricket_teams()
22
But when I run the code above I get output like:
JavaScript
1
2
1
{'Name': ['Sri lanka'], 'won': [1], 'lose': [2]}
2
I only get the result of the latest team. What should I do to assign multiple values to key?
if there are two teams the expected output should be:
JavaScript
1
2
1
{'Name': ['Sri lanka,Australia '], 'won': [1,2], 'lose': [2,2]}
2
Advertisement
Answer
Like this ,
JavaScript
1
20
20
1
def cricket_teams():
2
no_of_teams = int(input("Please enter the number of the teams: "))
3
4
main_dic = {}
5
main_dic["Team"] = []
6
main_dic["won"] = []
7
main_dic["lose"] = []
8
for i in range(0, no_of_teams):
9
name = str(input("Please enter the name of team: "))
10
won_ = int(input("How many time this team won a match: "))
11
lose_ = int(input("How many times this lose a match: "))
12
main_dic["Team"].append(name)
13
main_dic["won"].append(won_)
14
main_dic["lose"].append(lose_)
15
16
print(main_dic)
17
18
19
cricket_teams()
20