2016-11-16 6 views
0

私はPythonでジャンブル・ワード・ゲームを作ったので、スコアを最高から最低までソートしたいと思っています。スコアを記録するために私は棚を使用しましたが、棚を使用するときにキーとして並べ替える方法はわかりません。私はpickleを使うことができると知っていますが、誰かが知っていれば、これを棚で解決する方法はありますか? ありがとうございます!棚を使用してキーでスコアを並べ替える

def game(): 
    shelf=shelve.open("wordlists.dat") 
    listname = list(shelf.keys()) 
    for i in listname: 
     print("{}--{}".format(listname.index(i)+1,i)) 
    choice = int(input("Pick one:")) 
    word_set=(listname[choice-1]) 
    global wordlist 
    wordlist = shelf[word_set] 
    score = 0 
    #Choosing a random word from the wordlist 
    for i in range(4): 
     word = random.choice(wordlist) 
     theWord = word 
     jumble = "" 
     #Jumbling the word 
     while(len(word) > 0): 
      position = random.randrange(len(word)) 
      jumble += word[position] 
      word = word[:position] + word[position + 1:] 
     print("The jumble word is: {}".format(jumble)) 
     # Getting player's guess 
     guess = input("Enter your guess: ").lower() 
     # Congratulate the player 
     if(guess == theWord): 
      print("Congratulations! You guessed it") 
      score += 1 

     else: 
      print("Sorry, wrong guess.") 
    #Printing the score 
    print("You got {} out of 10".format(score)) 
    #Recording the score 
    shelf = shelve.open("score.dat") 
    score = str(score) 
    shelf.sync() 
    shelf.close() 
    return menu() 

def score(): 
    myformat = "{:10s} {:13s}" 
    print(myformat.format('Score', 'Date')) 
    print("-"*26) 
    shelf = shelve.open("score.dat") 
    for key in shelf.keys(): 
     dates = shelf[key] 
     for val in dates: 
      print(myformat.format(key, val)) 
    shelf.close 

出力:

Score  Date   
-------------------------- 
0   Wed Nov 16 12:07:28 2016 
2   Wed Nov 16 12:16:14 2016 
4   Wed Nov 16 12:16:42 2016 
1   Wed Nov 16 12:01:19 2016 
+1

棚は、本質的にちょうど永続的な辞書であり、彼らのように、ソートすることができない(そしてそれはキーがに必要とは何の関係もありません文字列である)。あなたができる最善のことは、すべてのキーのリストを取得し、ソートし、それを使ってソートされた順序で対応する値にアクセスすることです。実際には、 'pickle'と一緒に通常の辞書を使用して、それを保存し復元することもできます。 – martineau

答えて

-1

が、私はそれを考え出しました。私はキーをリストに入れ、バブルでソートしました。 これは、最終的なコードです:

def score(): 
    myformat = "{:10s} {:13s}" 
    print(myformat.format('Score', 'Date')) 
    print("-"*26) 
    shelf = shelve.open("score.dat") 
    listname = list(shelf.keys()) 
    #print(listname) 
    for num in range(len(listname)-1,0,-1): 
     for i in range(num): 
      if listname[i]>listname[i+1]: 
       temp = listname[i] 
       listname[i] = listname[i+1] 
       listname[i+1] = temp 
    for key in listname: 
     dates = shelf[key] 
     for val in dates: 
      print(myformat.format(key, val)) 
    shelf.close 
    return menu() 

出力:

Score  Date   
-------------------------- 
0   Wed Nov 16 18:08:53 2016 
1   Wed Nov 16 18:04:57 2016 
2   Wed Nov 16 18:05:11 2016 
3   Wed Nov 16 18:05:44 2016 
4   Wed Nov 16 18:04:44 2016 
+0

'list.sort()'はあなたのためにそれをソートします...あなたはどのようにソートすべきかを指定することもできます(http://stackoverflow.com/questions/36139/how-do-i-sort-a-list -of-strings-in-python)を使用します。 – TemporalWolf

+0

あなたは基本的な考え方を持っていますが、@TemporalWolfが指摘しているように、既に書き込まれ、デバッグされている以外に、バブルソートよりも速いPythonの組み込みソート方法の1つを使う方が良いでしょう。 – martineau

+0

はい、わかっています。しかし、Pythonの初心者として私はまだ学んでおり、組み込みメソッド以外の方法を使いたいと思っています。 –

関連する問題