highscores list
Chris
cwitts at gmail.com
Sat Dec 8 15:07:27 EST 2007
On Dec 8, 8:32 pm, Shawn Minisall <trekker... at comcast.net> wrote:
> I'm writing a game that uses two functions to check and see if a file
> called highScoresList.txt exists in the main dir of the game program.
> If it doesn, it creates one. That part is working fine. The problem is
> arising when it goes to read in the high scores from the file when I
> play again.
>
> This is the error msg python is giving me
>
> Traceback (most recent call last):
> File "<pyshell#0>", line 1, in <module>
> main()
> File "I:\PYTHON\PROJECT #3\PROJECT3.PYW", line 330, in main
> if(hasHighScore(wins) == True):
> File "I:\PYTHON\PROJECT #3\PROJECT3.PYW", line 175, in hasHighScore
> scores[i],names[i] = string.split(line,"\t")
> ValueError: need more than 1 value to unpack
>
> Here's the relavant code:
>
> def hasHighScore(score):
> #opens highScoresList.txt
> infile = open("highScoresList.txt",'r')
> scores = [0,0,0]
> names = ["","",""]
>
> #reads in scores from highScoresList.txt
> i=0
> for line in infile.readlines():
> scores[i],names[i] = string.split(line,"\t")
> names[i]=string.rstrip(names[i])
> i += 1
> infile.close()
>
> #compares player's score with those in highScoresList.txt
> i=0
> for i in range(0,len(scores)):
> if(score > int(scores[i])):
> return True
> else:
> return False
>
> def setHighScores(score,name):
> #opens highScoresList.txt
> infile = open("highScoresList.txt",'r')
> scores = [0,0,0]
> names = ["","",""]
>
> #reads in scores from highScoresList.txt
> i=0
> for line in infile.readlines():
> scores[i],names[i] = string.split(line,"\t")
> scores[i]=int(scores[i])
> names[i]=string.rstrip(names[i])
> i += 1
> infile.close()
>
> #shuffles thru the highScoresList.txt and inserts player's score if
> higher then those in file
> i=len(scores)
> while(score > scores[i-1] and i>0):
> i -= 1
>
> scores.insert(i,score)
> names.insert(i,name)
> scores.pop(len(scores)-1)
> names.pop(len(names)-1)
>
> #writes new highScoresList.txt
> outfile = open("highScoresList.txt","w")
>
> outfile.write (" High Score Name \n")
> outfile.write ("-------------------------------------------------\n")
>
> i=0
> for i in range(0,len(scores)):
> outfile.write("\t" + str(scores[i]) + "\t\t\t" + names[i] + "\n")
> outfile.close()
>
> And here's the call to the functions at the end of my game, included in
> the error msg.
>
> #adds player's score to high score list if high enough
> if(hasHighScore(wins) == True):
> setHighScores(wins,getName(wins))
>
> And this is what the text file looks like when it happens.
>
> High Score Name
> -------------------------------------------------
> 15 SHAWN
> 0
> 0
>
> The answer is probably simple, I've just been working on this program so
> long that my brain has turned to mush. Any help would be much
> appreciated...thanks.
Your first two lines in your highscores file has no Tab Characters.
When reading the file you could do:
for (i, line) in file_input:
if i < 2:
continue
# do normal file parsing
There are better ways to structure your code though, but that's for
you.
More information about the Python-list
mailing list