Hello! I have been making a "guess what number the computer thinks of"-game in python (text-only) for educational reasons. Now, i want to implement a highscore in the code, which writes the best three players with names and number of attempts. First, I'll just show you my game, but nevermind the code in that one:
Explanation: The notes are just explaining what the different sections does. nevermind those at all - they are in norwegian because i am a norwegian^^
Forsøk (in english attempts/tries) indicates how many attempts the user have used on guessing the right number. I did not plan on getting a highscore for this game, but now I've changed my mind. However, the integration of the actual highscore-code might be a little harder then. (Am I right?:P)
Now, to the actual highscore-code. I found a highscore from a previous post in here, but I am rather new to Python, and therefore had some trouble using it. Well, here is it (a little bit modifyed, as i tried to make it work on my own):
Can anyone help me out a little?;)
Code:
#Importere modulen random:
import random
#thenumber tilsvarer nå random.randint(0, 100) som er et tilfeldig tall mellom 0 og 100)
thenumber = random.randint(0, 100)
# Et tappert forsøk på en loop.
loop = 1
#Definerer choice.
choice = 0
forsøk = 0
#Så til selve gjettedelen av scriptet :)
print "It is a number between 0 and 100. Can you guess it?"
print "Guess-the-number-game made by CromogliC"
print "Just type in a number of your own choice and press enter!"
yourname = raw_input("Please type in your name here: ")
while loop == 1:
choice = input("What number do you think is is?: ")
if choice == thenumber:
forsøk = forsøk +1
print "Amazing," + " " + yourname + "!"
print "You have guessed the right number!"
print "You are victorious!"
print "Tries used:"
print forsøk
wins = forsøk
loop = 0
print "Restart? y for yes. n for no."
restart = raw_input("Do you want to restart?: ")
if restart == "y":
forsøk = 0
#User restart!
loop = 1
elif restart == "n":
loop = 0
print "Game Over!"
exit()
elif choice < thenumber:
print "Nope, you have got to go higher!"
forsøk = forsøk +1
loop = 1
elif choice > thenumber:
print "Nope, you have got to go lower!"
forsøk = forsøk +1
loop = 1
elif choice > 100:
print "I am sorry, but you cannot pick a number above 100!"
loop = 1
Forsøk (in english attempts/tries) indicates how many attempts the user have used on guessing the right number. I did not plan on getting a highscore for this game, but now I've changed my mind. However, the integration of the actual highscore-code might be a little harder then. (Am I right?:P)
Now, to the actual highscore-code. I found a highscore from a previous post in here, but I am rather new to Python, and therefore had some trouble using it. Well, here is it (a little bit modifyed, as i tried to make it work on my own):
Code:
def hasHighScore(score):
#opens guessnumberhighscore.txt
infile = open("guessnumberhighscore.txt",'r')
scores = [0,0,0]
names = ["","",""]
#reads in scores from guessnumberhighscore.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 guessnumberhighscore.txt
i=0
for i in range(0,len(scores)):
if(score in(scores[i])):
return True
else:
return False
def setHighScores(score,name):
#opens guessnumberhighscore.txt
infile = open("guessnumberhighscore.txt",'r')
scores = [0,0,0]
names = ["","",""]
scores = [0,0,0]
#reads in scores from guessnumberhighscore.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 guessnumberhighscore.txt and inserts player's score if
#higher than 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 guessnumberhighscore.txt
outfile = open("guessnumberhighscore.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()
#adds player's score to high score list if high enough
if(hasHighScore(wins) == True):
setHighScores(wins,yourname(wins))
Comment