So, I am trying to use python to write a high scores text file that can be read later when someone else opens the program. I have done this before in C++ and it looks like this:
void main()
{
//Declare local variables
int High_Score[5];
int Rank;
string High_Score_Name[5];
//Initialize a high score at 0
High_Score[4] = 0;
//Input the high scores from a file
ifstream Input_High_Scores;
Input_High_Scores.open("High_Scores.txt");
for (int i = 0; i < 5; i++)
{
Input_High_Scores >> High_Score[i];
Input_High_Scores >> High_Score_Name[i];
}
Input_High_Scores.close();
if (High_Score[4] == 0)
{
//Initialize local variables
High_Score[0] = 999999999;
High_Score[1] = 40000;
High_Score[2] = 37000;
High_Score[3] = 30000;
High_Score[4] = 25000;
High_Score_Name[0] = "Developer";
High_Score_Name[1] = "Adam";
High_Score_Name[2] = "Nastasia";
High_Score_Name[3] = "Nicolas";
High_Score_Name[4] = "Dani";
}
}
What I need is a version that does what this does in Python. I have found a way to save 1 value in a text file, but I want it to save 10 values in total. 5 names and 5 scores for those names. I also need to know how to read those values in the program. Here is what I have so far:
# Trivia game created by NeverEndingCycle
Start = ""
Exit = ""
Score = int(0)
Next = ""
HighScore = int(0)
def GetHighScore():
# Default high score
HighScore = 0
# Try to read the high score from a file
try:
high_score_file = open("high_score.txt", "r")
HighScore = int(high_score_file.read())
high_score_file.close()
print("The high score is", HighScore)
except IOError:
# Error reading file, no high score
print("There is no high score yet.")
print("")
except ValueError:
# There's a file there, but we don't understand the value.
print("I'm confused. Starting with no high score.")
return HighScore
def save_high_score(new_high_score):
try:
# Write the file to disk
high_score_file = open("Highscore.txt", "w")
high_score_file.write(str(new_high_score))
high_score_file.close()
except IOError:
# Hm, can't write it.
print("ERROR --- Unable to save the high score.")
def CheckForHighScore():
global Score
# Get the high score
high_score = GetHighScore()
# Get the score from the current game
current_score = 0
try:
current_score = Score
except ValueError:
# Error
print("___ERROR___")
# See if we have a new high score
if current_score > high_score:
# We do! Save to disk
global Exit
print("Yea! New high score!")
save_high_score(current_score)
Exit = input("Press ENTER to exit")
else:
global Exit
print("No new Highscore, Better luck next time.")
Exit = input("Press ENTER to exit")
def ShowScore():
global Score
global Next
print("")
print("You now have", Score, "Point(s).")
Next = input("Press ENTER to continue to the next Question.")
def Q1():
global Score
print("")
print("Question 1.")
print("What type of variable is a String? (1 Point)")
print("")
print("1. Letters")
print("2. Numbers")
print("3. Decimals")
print("4. A String is not a varible.")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 1
print("")
print("Correct! 1 point has been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q2():
global Score
print("")
print("Question 2.")
print("(1 Point)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 1
print("")
print("Correct! 1 point has been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q3():
global Score
print("")
print("Question 3.")
print("(1 Point)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 1
print("")
print("Correct! 1 point has been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q4():
global Score
print("Question 4.")
print("(1 Point)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 1
print("")
print("Correct! 1 point has been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q5():
global Score
print("Question 5.")
print("(5 Points)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 5
print("")
print("Correct! 5 points have been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q6():
global Score
print("")
print("Question 6.")
print("(2 Points)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 2
print("")
print("Correct! 2 points have been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q7():
global Score
print("")
print("Question 7.")
print("(2 Points)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 2
print("")
print("Correct! 2 points have been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q8():
global Score
print("")
print("Question 8.")
print("(2 Points)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 2
print("")
print("Correct! 2 points have been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q9():
global Score
print("Question 9.")
print("(2 Points)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 2
print("")
print("Correct! 2 points have been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Q10():
global Score
print("")
print("Question 10.")
print("(10 Points)")
print("")
print("1. ")
print("2. ")
print("3. ")
print("4. ")
print("")
Answer = input("What is your answer? ")
if Answer == "1":
Score = Score + 10
print("")
print("Correct! 2 points have been added to your score!")
print("")
elif Answer == "2":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "3":
print("")
print("Incorrect. No points were added.")
print("")
elif Answer == "4":
print("")
print("Incorrect. No points were added.")
print("")
else:
print("Incorrect. Choose an answer between 1 and 4 next time!")
return Score
def Main():
global Next
Q1()
ShowScore()
Q2()
ShowScore()
Q3()
ShowScore()
Q4()
ShowScore()
print("")
print("Bonus Question 5x Points!")
Q5()
ShowScore()
print("")
print("Last 5 questions! All points are double!")
Q6()
ShowScore()
Q7()
ShowScore()
Q8()
ShowScore()
Q9()
ShowScore()
print("")
print("Final Question 5x Points!")
Q10()
ShowScore()
Next = input("Press ENTER to continue.")
Name = input("Please enter your name: ")
print("")
print("Welcome, %s, to NeverEndingCycle's Trivia Game!" % (Name))
print("")
Start = input("Press ENTER to begin playing!")
print("")
Main()
CheckForHighScore()
Lastly, if I could keep as much of the original code as possible, that would be great. I am trying to teach myself and it has gone well until now. What I mean is I need to understand whatever the solution is to my problem or I will not know how to fix it in the future.
I am very new to python-- like started last week new -- and I am teaching myself, so I apologize if this is a stupid question, but I could not find a solution anywhere. Also, I know my formatting is terrible, I'm working on it.