Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1_12_maths_game_highscore_table.py
- # Deal with input
- """
- . First, try the code with no highscores.txt file.
- . Does the game create the file?
- A. prints error message: No highscores file found
- and creates highscores.txt
- . Does the game save the name and scores correctly each time the game loops? Remember to test different scores!
- A.And writes names scores.
- . Does the high score table work as expected?
- A. Yes. Though I don't understand the positioning.
- """
- print("Loading high scores")
- scores = []
- names = []
- try:
- with open("highscores.txt", "r") as f:
- for line in f:
- line = line.strip("\n")
- line = line.split(" ")
- names.append(line[0])
- scores.append(int(line[1]))
- except FileNotFoundError:
- print("No highscores file found")
- while True:
- # display the highscores
- print("Highscores")
- for pos in range(len(names)):
- print(pos + 1, names[pos], scores[pos])
- score = 0
- print("Welcome to the Maths Quiz")
- print("Can you answer three questions and score maximum points?")
- name = input("Whats your name? ")
- print("Question 1: What is the product of 2x2x2?")
- answer = int(input("Your answer :>> "))
- if answer == 8:
- print("Correct, That is 1 mark for you.")
- score = score + 1
- else:
- print("Incorrect, the answer is 8")
- print("Question 2: What is the product of 3x3 ? ")
- answer = int(input("Your answer :>> "))
- if answer == 9:
- print("Correct, Another 1 point scored.")
- score = score + 1
- else:
- print("Incorrect, the answer is 9")
- # print("Question 3: What is the quotient and remainder for 8 / 3?")
- # print("Please format your answer as quotient and remainder, for example the answer to 23/5 is 4r3")
- print("Question 3: What is the product of 5x2 ?")
- """ For Text Answers >
- answer = input("Your answer :>> ")
- if answer == "2r2": """
- answer = int(input("Your answer :>> "))
- if answer == 10:
- print("Correct, you have scored another 1 point.")
- score = score + 1
- else:
- print("Incorrect, the answer is 10")
- print("Your score is", score)
- # add the new score to the highscores
- position = 0
- for compare_score in scores:
- if score < compare_score:
- position = position + 1
- scores.insert(position, score)
- names.insert(position, name)
- scores = scores[:10]
- names = names[:10]
- # output the new highscore
- print("Saving highscores")
- with open("highscores.txt", 'w') as f:
- for pos in range(len(names)):
- f.write(names[pos] + " " + str(scores[pos]) + "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement