Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1_10_mathsgame_saving2.py TM edit ; basics from futurelearn
- # 1_10_highscore.txt" must contain an integer or the code bombs:
- """ >>> '1_10_mathsgame_saving 2.py'
- Traceback (most recent call last):
- File "/home/tony/Documents/_Python 103/w1/W1Scripts/1_10_mathsgame_saving 2.py", line 7, in <module>
- highscore = int(highscore)
- ValueError: invalid literal for int() with base 10: '' """
- # So how to ensure that the file has '0' or something else?
- # New problem. fh should do a 'w' write ie an Overwrite. but it is saving 3000,
- # then on a rerun saves 3000300030003000.
- # Then 3000300030003000300030003000300030003000300030003000300030003000
- # What is wrong? Sheepish grin.
- # note this is python3.8 on xubuntu and using Thonny ,ok and Pycharm CE, won't run.
- with open("1_10_highscore.txt", "r") as fh: # NB must have content; 0 or some int.
- highscore = fh.read()
- highscore = int(highscore)
- print("The highscore at start is", highscore) # OK
- fa = open("1_10_score.txt", "a") # file may be empty !
- fh = open("1_10_highscore.txt", "w")
- while True:
- score = 0
- print("Welcome to the Maths Quiz")
- print("Can you answer three questions and score maximum points?")
- fa.write(str(score))
- fh.write(str(highscore)) # this overwrites the initial 0 with nothing ?
- print("files written #0")
- print("Question 1: What is the product of 2x2x2?")
- answer = int(input("Your answer :>> "))
- if answer == 8:
- print("Correct")
- score = score + 1
- print("For Question 1 you earned 1 for a score of:", score)
- fa.write(str(score) + "\n")
- fh.write(str(highscore))
- else:
- print("Incorrect, the answer to Question 1 is 8")
- # break
- print("Question 2: What is the product of 3x3 ?")
- answer = int(input("Your answer :>> "))
- if answer == 9:
- print("Correct")
- score = score + 1
- print("For Question 2 you earned another 1 for a score of:", score)
- fa.write(str(score) + "\n")
- fh.write(str(highscore))
- else:
- print("Incorrect, the answer is 9 ")
- # break
- print("Question 3: What is the product of 5x2 ?")
- # answer = int(input("Your answer :>> "))
- answer = input("Your answer :>> ")
- if answer == "10":
- print("Correct")
- score = score + 1
- print("For Question 3 you earned another 1 for a score of", score)
- fa.write(str(score) + "\n")
- fh.write(str(highscore))
- else:
- print("Incorrect, the answer is 10")
- print("Your score is", score)
- if score >= highscore:
- highscore = score
- print("You have set a new highscore of:", highscore)
- with open("1_10_highscore.txt", "w") as fh:
- fh.write(str(highscore))
- # with open("1_10_score.txt", "a") as fa:
- # fa.write(str(score) + "\n")
- else:
- print("Better luck next time")
- print("The highscore is", highscore)
- fa.close()
- fh.close()
- break
- # Add more questions. ... OK
- # "Question 1: What is the product of 2x2x2 ?", 8
- # "Question 2: What is the product of 3x3 ?", 9
- # "Question 3: What is the product of 5x2 ?", 10
- # Save running score .. OK.
- # Save high score ... unsatisfactory.
- # Handle the incorrect responses to the questions.
- # now 1_10_highscore.txt was "0" looks like this "3000"
Add Comment
Please, Sign In to add comment