そのため、Pythonを使用して、後で誰かがプログラムを開いたときに読み取ることができる高スコアのテキストファイルを作成しようとしています。私は以前にC ++でこれを実行しましたが、次のようになります。
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";
}
}
私が必要としているのは、これがPythonで行うことを行うバージョンです。テキストファイルに1つの値を保存する方法を見つけましたが、合計10の値を保存したいと思います。5つの名前とそれらの名前の5つのスコア。また、プログラムでこれらの値を読み取る方法も知っておく必要があります。これが私がこれまでに持っているものです:
# 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()
最後に、元のコードをできるだけ多く保持できれば、それは素晴らしいことです。私は自分自身を教えようとしています、そしてそれは今までうまくいっています。私が言いたいのは、私の問題の解決策が何であるかを理解する必要があるということです。そうしないと、将来それを修正する方法がわかりません。
私はPythonに非常に慣れていないので、先週新しく始めたように、自分で教えているので、これがばかげた質問であるかどうかをお詫びしますが、どこにも解決策が見つかりませんでした。また、私のフォーマットがひどいことを知っています、私はそれに取り組んでいます。
ファイルへのデータの追加
このコードはファイルにデータを追加するために使用できるため、ファイルが存在する場合でも現在のデータを上書きすることはありません。「a +」は追加を表し、プラス記号は、ファイルがまだ存在しない場合にファイルを作成することを意味します。
high_score_file = open("Highscore.txt", "a+")
ハイスコア情報をファイルに書き込むには、コードの最初の行を変更するだけです。「w」の代わりに「a +」を参照してください。
high_score_file = open("Highscore.txt", "a+")
high_score_file.write(str(new_high_score)
high_score_file.close()
ファイルからのデータの読み取り
ファイルからのデータの読み取りは簡単で、これはデータを読み取る効率的な方法です。データを1行ずつ読み取ります。また、一番上の行は読み取り専用モードでファイルを開いているので、「r」を指定します。これはあなたがそれに書くことができないことを意味します。
high_score_file = open("Highscore.txt", "r")
for line in high_score_file:
print(line, end='')
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加