2017-05-06 60 views
-2

好吧,所以我希望当用户想要检查高分时,输出会按降序打印数据请记住,都是.txt文件中的名称和数字,这就是为什么我觉得这很难。如果你还需要什么,请告诉我,在我将如何阅读.txt文件,然后按降序排列数据

def highscore(): 
     global line #sets global variable 
     for line in open('score.txt'): 
      print(line) 
    #================================================================================= 
    def new_highscores(): 
     global name, f #sets global variables 
     if score >= 1:#if score is equal or more than 1 run code below 
      name = input('what is your name? ') 
      f = open ('score.txt', 'a') #opens score.txt file and put it into append mode 
      f.write (str(name)) #write name on .txt file 
      f.write (' - ') #write - on .txt file 
      f.write (str(score)) #write score on .txt file 
      f.write ('\n') #signifies end of line 
      f.close() #closes .txtfile 
     if score <= 0: #if score is equal to zero go back to menu 2 
      menu2() 

我加入这个以防万一有在我在文件

+0

你如何运行它,你从哪里调用这些函数? – davedwards

+0

是的,我可以添加完整的代码,如果你想 – Jesuspepper

+0

@Jesuspepper你看看我的答案?如果您对此有任何疑问或者无法使用,请添加评论,但我认为它应该满足您的需求。 –

回答

0

做最简单的事情写的方式有问题只是维持高分数文件处于排序状态。这种方式每次你想打印出来,只要继续做就可以了。当您添加分数时,再次对列表进行排序。下面是new_highscores一个版本,实现这一点:

def new_highscores(): 
    """"Adds a global variable score to scores.txt after asking for name""" 
    # not sure you need name and f as global variables without seeing 
    # the rest of your code. This shouldn't hurt though 
    global name, f # sets global variables 
    if score >= 1: # if score is equal or more than 1 run code below 
     name = input('What is your name? ') 

     # here is where you do the part I was talking about: 
     # get the lines from the file 
     with open('score.txt') as f: 
      lines = f.readlines() 

     scores = [] 
     for line in lines: 
      name_, score_ = line.split(' - ') 
      # turn score_ from a string to a number 
      score_ = float(score_) 
      # store score_ first so that we are sorting by score_ later 
      scores.append((score_, name_)) 

     # add the data from the user 
     scores.append((score, name)) 
     # sort the scores 
     scores.sort(reverse=True) 

     # erase the file 
     with open('score.txt', 'w') as f: 
      # write the new data 
      for score_, name_ in scores: 
       f.write('{} - {}\n'.format(name_, score_))   

    if score <= 0: # if score is equal to zero go back to menu 2 
     menu2() 

你我使用的是with声明通知。您可以了解更多有关here,但本质上它是这样工作的:

with open(filename) as file: 
    # do stuff with file 
# file is **automatically** closed once you get down here. 

即使你离开块的另一个原因(一个异常被抛出,你从一个函数返回初等)使用with语句是处理文件的一种更安全的方式,因为你基本上是让Python为你处理文件的关闭。 Python永远不会像程序员那样忘记。

你可以阅读更多关于拆分和格式herehere

PS,有一个名为binary search方法会更有效,但我得到你刚开始这样的感觉,我想保持简单。基本上你要做的是在文件中搜索新分数的位置,通过在每个点处将搜索区域减半来插入新分数。然后当你写回文件时,只写出不同的东西(从新的分数开始)

+0

我已经添加了我的代码的其余部分,如果你想看看它,因为我觉得我没有提供你所需要的一切。还有什么我需要改变def_highscore? – Jesuspepper

+0

'high_score'函数应该没问题。你有没有更新你的'new_highscores'来匹配我的?如果您遇到问题,请详细说明哪些功能无效。此外,您需要了解我已经提供的代码。如果你甚至没有阅读我的答案足以理解你应该如何使用它,我无法帮助你。 –

+0

是的,我已经更新它,但想更详细地通过它,我会保持你与我有任何问题张贴 – Jesuspepper