简体   繁体   中英

Print variables in order based on pseudo-random numbers

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!

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM