Hi guys
I finally finished my highscore list but i have one problem I have to sort it.
I know how to sort regular list, but this is a bit different because of having the text in it. When I sort it I just get an weirdly sorted list. Does anyone one know how to properly sort it.
Here is my code:
## ~-~-~-~-~-~-~-~-~-~-~Pre MainLoop Declarations ~-~-~-~-
import csv
import string
import random
from HMGraphics import HangingMan
bKeepPlaying = True
## ~-~-~-~-~-~-~-~-~-~-~ Draws hangman to the screen base ~-~-~-~-
## ~-~-~-~-~-~-~-~-~-~-~ on how many error the play has ~-~-~-~-
def DrawErrors(iErrors):
print HangingMan[iErrors]
## ~-~-~-~-~-~-~-~-~-~-~ Main Game Loop ~-~-~-~-
while bKeepPlaying == True:
## ~-~-~-~-~-~-~-~-~-~-~ Various Declarations ~-~-~-~-
sWords= open("Dictionary.txt", "r")
sHighScoreRead = open("HighScore.txt","r")
sPrevHighScore = sHighScoreRead.readlines()
sRawText = sWords.read()
sWords=[]
iStart=0
iEnd=0
iIndex =-1
iWordNumber = 0
iErrors = 0
iScore = 0
iGuesses = 0
sLettersUsed = ""
## ~-~-~-~-~-~-~-~-~-~-~ Peel off words from list -~-~-
for char in sRawText:
iIndex += 1
if(char == "~" and iIndex < 1):
iEnd = iIndex
if(char == "~" and iIndex > 1):
iStart = iEnd
iEnd = iIndex
iWordNumber += 1
sWords.insert(iWordNumber,sRawText[iStart+1:iEnd])
## ~-~-~-~-~-~-~-~-~-~-~ Create copy of random word -~-~-
## ~-~-~-~-~-~-~-~-~-~-~ and write it as a series of dashes-
iRandomWord = int(random.random()*850)
sAnswer = sWords[iRandomWord]
sAnswer = list(sAnswer)
sReveal = ""
sReveal += len(sAnswer) * "-"
sReveal = list(sReveal)
print "".join(sAnswer)
print "".join(sReveal)
## ~-~-~-~-~-~-~-~-~-~-~ Loop that get a letter from the player ~-~-~-~-
## ~-~-~-~-~-~-~-~-~-~-~ and check if that letter exists in the correct word ~-~-~-~-
## ~-~-~-~-~-~-~-~-~-~-~ if it does it then replaces the dashes in the ~-~-~-~-
## ~-~-~-~-~-~-~-~-~-~-~ sReveal variable with the correct letter. ~-~-~-~-
while True:
sLetterGuess = str(raw_input("Please choose a letter "))
if sAnswer.count(sLetterGuess) == 0:
iErrors += 1
sLettersUsed += sLetterGuess
DrawErrors(iErrors)
print sLettersUsed
else:
for i in range(len(sReveal)):
if sAnswer[i]== sLetterGuess:
sReveal[i] = sLetterGuess
print "".join(sReveal)
sLettersUsed += sLetterGuess
print "letters used : " + sLettersUsed
if sReveal.count("-") == 0: #if there are no more dashes the player has guessed the right word
print "You Won"
iScore += (10 - iErrors) #calculates the players score
sPlayAgain = raw_input("would you like to play again (y/n)? ")
if sPlayAgain == 'y':
break
else:
bKeepPlaying = False
sPlayerName = raw_input("Please enter your name for the highscore list: ")
break
elif iErrors >= 8: #if they guess more then 8 letters wrong they lose.
print "You Lost"
sPlayAgain = raw_input("would you like to play again (y/n)? ")
if sPlayAgain == 'y':
break
else:
bKeepPlaying = False
sPlayerName = raw_input("Please enter your name for the highscore list: ")
break
## ~-~-~-~-~-~-~-~-~-~-~-put highscore information in notepad and display the highscores~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
sPrevHighScore += '\n' + sPlayerName + ", " + str(iScore)
sHighScoreWrite = open("HighScore.txt","w")
sHighScoreWrite.writelines(sPrevHighScore)
print "Your score was " + str(iScore)
print "These are the highscores"
print "".join(sPrevHighScore)
print "Thanks for playing!"
sHighScoreRead.close()#Closes the highscore list to clear the memory
sHighScoreWrite.close()
## ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# PLAY AGAIN COMMENTS
## ~-~-~-~-~-~-~-~-~-~-~ Asks the player if they want to play again -~-~-
## ~-~-~-~-~-~-~-~-~-~-~ if they say no then it breaks out of loop -~-~-
## ~-~-~-~-~-~-~-~-~-~-~ and goes into main loop, but since the start condition -~-~-
## ~-~-~-~-~-~-~-~-~-~-~ (bKeepPlaying) will be equal to false, that loop will no longer run. -~-~-
## ~-~-~-~-~-~-~-~-~-~-~ if they say they do want to keep playing then the loop is -~-~-
## ~-~-~-~-~-~-~-~-~-~-~ broken out of, and we go back into the main game loop -~-~-
## ~-~-~-~-~-~-~-~-~-~-~ but since bKeepPlaying is equal to true it will still run. -~-~-
# PLAY AGAIN CODE
##sPlayAgain = raw_input("would you like to play again (y/n)? ")
##if sPlayAgain == 'y':
## break
##else:
## bKeepPlaying = False
## sPlayerName = raw_input("Please enter your name for the highscore list: ")
## break
Also i would appreciate any other suggestions to make my code better.