如何使用玩家的分数和姓名制作高分列表

时间:2018-12-27 12:16:17

标签: python python-3.x

我正在制作一个高分模块,该模块目前仅显示获得的前5个得分,我希望能够像我目前一样显示这些得分,而且还希望用与该得分相关的名称来显示它们我会这样做吗?

到目前为止,这是我的代码:

<?php
$init_array = array(
array('kode_pemesanan'=>'FPS26122018001', 
    'nama'=>'bintang', 
    'value1'=>'A Pieu pastel blusher (Shade 1)', 
    'value2'=>'1', 
    'value3'=>'50000', 
    'value4'=> '9000',
    'value5'=> '59000',
),
array('kode_pemesanan'=>'FPS26122018001', 
    'nama'=>'bintang', 
    'value1'=>'A Pieu pastel blusher (Shade 3)', 
    'value2'=>'1', 
    'value3'=>'10000', 
    'value4'=> '9000',
    'value5'=> '59000',
),
array('kode_pemesanan'=>'FPS26122018002', 
    'nama'=>'bintang', 
    'value1'=>'A Pieu pastel blusher (Shade 3)', 
    'value2'=>'1', 
    'value3'=>'10000', 
    'value4'=> '9000',
    'value5'=> '59000',
),
);

$formatted_array = array();

foreach($init_array as $element){
$formatted_array[$element['kode_pemesanan']][] = $element;
}
?>

<table border='2' width="70%" align="center">
<?php foreach($formatted_array as $row ): ?>
<tr>
 <td rowspan="<?=count($row)?>"><?=$row[0]['kode_pemesanan']?></td>
 <td rowspan="<?=count($row)?>"><?=$row[0]['nama']?></td>
 <?php foreach( $row as $key => $value ): ?>
  <td><?=$value['value1']?></td>
  <td><?=$value['value2']?></td>
  <td><?=$value['value3']?></td>
  <?php if($key == 0): ?>
  <td rowspan="<?=count($row)?>"><?=$value['value4']?></td>
  <td rowspan="<?=count($row)?>"><?=$value['value5']?></td>
  <?php endif; ?>
  </tr><tr>
 <?php endforeach; ?>
 </tr>
 <?php endforeach; ?>
 </table>

任何帮助将不胜感激,因为我需要将此代码用于学校作业。

2 个答案:

答案 0 :(得分:1)

我们在这里

scores = []
with open('scores.txt') as f:
    for line in f:
        name, score = line.strip().split()
        scores.append ((name, int(score)))

    # get top 5
    sorted_scores = sorted(scores, key=lambda x: x[1], reverse=True)

    print('The First 5 Highscores are:')
    for item in sorted_scores[:5]:
        print(item)

scores.txt

John 100
Eduard 200
Phil 150
Romeo 500
Mark 50
Ann 1

输出

('Romeo', 500)                                                                  
('Eduard', 200)
('Phil', 150)                                                                   
('John', 100)                                                                   
('Mark', 50) 

答案 1 :(得分:0)

您可以做的是,不用列出包含分数的列表,而是使用具有(分数,名称)的元组列表并按分数对它进行排序。

def highscore():
   file_highscore = open('scores_test2.txt' , 'r')
   scores = []
   for line in file_highscore.readlines():
      score_info = line.split()
      scores.append((score_info[0], int(score_info[1])))

   scores.sort(key = lambda x:x[1], reverse = True)
   print('The First 5 Highscores are:')
   for score, name in scores[:5]:
      print(name, score)