+ 3
Why this python program showing error. Can anyone answer
1={'name':'yash', 'role no.':21} 2={'name':'saurav', 'role no.':22} 3={'name':'ram', 'role no.':23} For I in range(1,4) Print(i.get('name'))
22 odpowiedzi
+ 14
The main errors are :
1) Numbers cannot be used as variable name.
2) Python is case-sensitive. You've used uppercase letters.
And yes, another one: you've used for loop but the colon seems to be dead.
+ 10
Do you have to use dictionaries, is that what the assignment says? I feel like other types might be more suitable for what you're trying to do. Personally, I'd go with a namedtuple:
from collections import namedtuple
Player = namedtuple('Player', ('name', 'role', 'runs', 'four', 'six', 'balls', 'field')) # etc.
players = [
Player('Virat Kohli', 'bat', 112, 10, 0, 119, 0),
Player('du Plessis', 'bat', 120, 11, 2, 112, 0),
]
calculate_points = lambda player: player.runs // 2 # etc.
# print players sorted by points in descending order:
for player in sorted(players, key=calculate_points, reverse=True):
print(f'{player.name} ({player.role}): {calculate_points(player)} points')
+ 5
Yeah, not dictionary. They should be placed inside a list.
+ 5
hi, you don’t need to put a dict inside a list, because you can iterate through it:
students = {'Mary':[20,45,12], 'Grace':[40,78,56],'John':[61,37,580]}
for name, score in students.items():
print(f'Name: {name} Score: {score}')
+ 4
It's because variable names cannot start with a digit, because then you could confuse numbers with variables, and that would be really bad
+ 3
for player in players:
calculate_points(player)
+ 2
If you put them in a list, you can, but otherwise there isn't any clean way to do it
+ 1
p1={'name':'Virat Kohli', 'role':'bat', 'runs':112, '4':10, '6':0,
'balls':119, 'field':0}
p2={'name':'du Plessis', 'role':'bat', 'runs':120, '4':11, '6':2,
'balls':112, 'field':0}...
I am making like dream11 code . So, Storing information of many players in dictionaries. So there must be a looping involved to calculate point earned by each player. I am stuck in that?
+ 1
To add to what others have said, if you want to calculate a value for the number of points each player has scored, it would be advisable to create a function to do such a thing. Maybe something like:
def calculate_points(player):
points = 0
points += player['runs']
points += 10*player['wickets']
points += player['field']
return points
Obviously you can define your own scoring method. Then when looping, you can say
for player in players:
print(player['name'], calculate_points(player))
which would appear something like
Virat Kohli 112
Faf du Plessis 120
...
You do (as others have told you) need to add each player to a list in order to loop over them. You can do as you have done (p1 = {'name':'Virat Kohli', ...}, etc) and then have your players list as
players = [p1, p2, p3, ...]
Good luck!
+ 1
p1={'name':'Virat Kohli', 'role':'bat', 'runs':112, '4':10, '6':0,
'balls':119, 'field':0}
p2={'name':'du Plessis', 'role':'bat', 'runs':120, '4':11, '6':2,
'balls':112, 'field':0}...
I had made a function to calculate the point. but in function calculation i need to access the dictionaries which i made for a match as like above . various criteria of giving point and for that i need to access the value from dictionary while i am inside the for loop at the same time like that.
for ......
function to calculate point.
in this function i need p1.get('runs') , p2.get('overs') p3.get('runs'){vary for each player}like all values for each player to calculate point. how will i got this.
+ 1
What criteria are you basing the player's points on? If it's something simple like the role, then you can do
def calculate_points(player):
if player['role'] == 'bat':
return player['runs']
elif player['role'] == 'bowl':
return player['wickets']
elif player['role'] == 'all-rounder':
return player['runs'] + player['wickets']
If it's a more complicated choice than that, you're just going to have to work out your algorithm to decide what your code displays.
+ 1
i have to do it for one match. but in function to calculate player stats i need to access the dictionary .
0
How can I access dictionaries using for loop
0
Yes, you are, I said you should use lists.
players = [
{"name":"Virat Kohli", "role":"bat", "runs":112...}...
]
You can then access it with for loops like this:
for i in players:
print(i.get("name"))
0
Batting
• 1 point for 2 runs scored
• Additional 5 points for half century
• Additional 10 points for century
• 2 points for strike rate (runs/balls faced) of 80-100
• Additional 4 points for strike rate>100
• 1 point for hitting a boundary (four) and 2 points for over boundary (six)
Bowling
• 10 points for each wicket
• Additional 5 points for three wickets per innings
• Additional 10 points for 5 wickets or more in innings
• 4 points for economy rate (runs given per over) between 3.5 and 4.5
• 7 points for economy rate between 2 and 3.5
• 10 points for economy rate less than 2
Fielding
• 10 points each for catch/stumping/run out
0
Ok, that's doable, but it's likely to look quite messy. You will need to have each player's stats from each game appearing separately. So I'm thinking 'runs' would be a list of each of their innings.
p1 = {'name':'Virat Kohli', 'runs':[84,23,105,67],...}
And in your points-calculating function, you can have
points += len([i for i in player['runs'] if i >= 50 and i < 100]
Or you could add an extra key in the dictionary of 'fifties' to sort that out. A similar approach would be necessary for wickets, economies, etc, i.e. needing stats for each individual game.
0
I think I finally understand what you are doing. So each player will have different keys in their dictionary depending on what they've done in that match. So Virat Kohli won't have an entry for 'balls' because he doesn't bowl, correct?
So then I think your calculate_points function will need to look something like this:
def calculate_points(player):
points = 0
if player.get('runs'):
points += player['runs']
if player['runs'] >= 100:
points += 10
elif player['runs'] >= 50:
points += 5
strike_rate = 100 * player['runs'] / player['balls']
if strike_rate >= 100:
points += 4
elif strike_rate >= 80:
points += 2
points += player['4'] + 2 * player['6']
if player.get('balls'):
... and so on.
The if player.get('balls') means that, if a player hasn't bowled it will skip over it happily without encountering an error whether you have {..., 'balls': 0,...} as part if your dictionary, or you just haven't entered it at all.
0
brother i had also done till here My problem is again i am telling. there is not a single player in match . there are 22 players and hence 22 dictionaries. i have to introduce looping . in looping i am unable to access the stuf inside each dictionary and calculate the points using the function.the above code looks good if only one player . but what if i had 22 players
0
As above, you need to put the players in a list.
So where you've done
p1 = {'name':'Virat Kohli', ...}
p2 = {...}
p3 = {...}
you then do
players = [p1, p2, p3, ...]
and if you want to display the players in order of the number of points they've scored, you can do as Anna has done, i.e.
for player in sorted(players, key=calculate_points, reverse=True):
print(f"{player['name']}: {player['role']} {calculate_points(player)}")
0
Errors:
1. Variables cannot start with digit.
2. In for and print function, python is case sensitive so, you must have to start with small letter in place of P and F.
3. You didn't used Semicolon for writing body of for loop