-2

I am creating a task where a person receives a score based on their accuracy. Then they are presented with a screen with their performance place among other players (where the other players' scores are randomly generated numbers.)

I need to generate random scores for the fake players, then somehow sort the scores in descending order based on their placement. The only real score that is presented will be [sub_score].

Below is what I have so far. I'm not sure how to sort these variables based on their values, then print the variables for [firstplace], [secondplace], [thirdplace], etc. Also, this needs to occur 4 times, where each time a different score is generated for the fake players, and their placement fluctuates across tasks. These randomly generated numbers should fall between 70-90 since these scores reflect accuracy.

import random
'jsw_score' = random.randint(70,90)
'jbp_score' = random.randint(70,90)
'bsp_score' = random.randint(70,90)
'mjk_score' = random.randint(70,90)
'phs_score' = random.randint(70,90)
'msw_score' = random.randint(70,90)
'tdl_score' = random.randint(70,90)
'aik_score' = random.randint(70,90)
'wjc_score' = random.randint(70,90)
'sub_score' = accuracy

Thank you!

2 Answers2

1

A couple of things: 1) Not sure what do you use sub_score for. 2) You need better data structure to avoid duplicated code. I used a dictionary below.

import random


dd = {}
for i in [
        'jsw_score',
        'jbp_score',
        'bsp_score',
        'mjk_score',
        'phs_score',
        'msw_score',
        'tdl_score',
        'aik_score',
        'wjc_score']:
    dd[i] = random.randint(70,90)

# sort by score
for item in sorted(dd.items(), key=lambda x: x[1], reverse=True):
    print item

example output:

('aik_score', 90)
('tdl_score', 89)
('jsw_score', 88)
('wjc_score', 87)
('msw_score', 84)
('jbp_score', 82)
('bsp_score', 81)
('mjk_score', 79)
('phs_score', 73)
zyxue
  • 7,904
  • 5
  • 48
  • 74
  • Nice dynamic construction of the dictionary in this answer. – Vincent Ramdhanie Aug 30 '15 at 16:28
  • Thank you for your response! This looks a somewhat similar to my current one. (I need the players' names to appear along with their scores.) I'm very new to python, so I'm flailing a bit! – user5278283 Aug 30 '15 at 17:19
  • P.S. The sub_score refers to the person's actual performance. So, they will receive their placement among the other fake players with randomly generated scores. (Accuracy is defined elsewhere in the program.) – user5278283 Aug 30 '15 at 17:45
0

Creating individual variables the way you are will make this task very difficult. It would also be hard to scale. Consider what would happen if you had a hundred scores, or a thousand!

Instead carefully consider your requirements and choose an appropriate data structure that would meet all your requirements. Many structures can even be sorted automatically.

Given that you have a set of keys: jsw_score, jbp_score etc lends itself to a dictionary structure to start with. You can statically construct a dictionary like this:

 scores = {'jsw_score':random.randint(70,90),
           'jbp_score':random.randint(70,90),
           ...}

Then take a look at the answers to this question concerning sorting a dictionary by values.

Alternatively, if you have other requirements that the dictionary does not help with then you can read this page in the docs and select an appropriate data structure.

Community
  • 1
  • 1
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192