TonyMo

1_10_mathsgame_saving2.py

Apr 14th, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. #  1_10_mathsgame_saving2.py TM edit ; basics from futurelearn
  2. # 1_10_highscore.txt" must contain an integer or the code bombs:
  3. """ >>>  '1_10_mathsgame_saving 2.py'
  4. Traceback (most recent call last):
  5.  File "/home/tony/Documents/_Python 103/w1/W1Scripts/1_10_mathsgame_saving 2.py", line 7, in <module>
  6.    highscore = int(highscore)
  7. ValueError: invalid literal for int() with base 10: ''  """
  8. # So how to ensure that the file has '0' or something else?
  9.  
  10. #  New problem. fh should do a 'w' write ie an Overwrite. but it is saving 3000,
  11. #  then on a rerun saves 3000300030003000.
  12. # Then 3000300030003000300030003000300030003000300030003000300030003000
  13. #  What is wrong? Sheepish grin.
  14. # note this is python3.8 on xubuntu and using Thonny ,ok and Pycharm CE, won't run.
  15.  
  16. with open("1_10_highscore.txt", "r") as fh:  # NB must have content; 0 or some int.
  17.     highscore = fh.read()
  18.     highscore = int(highscore)
  19.     print("The highscore at start is", highscore)   # OK
  20.    
  21. fa = open("1_10_score.txt", "a")  # file may be empty !
  22. fh = open("1_10_highscore.txt", "w")
  23.  
  24. while True:
  25.     score = 0
  26.     print("Welcome to the Maths Quiz")
  27.     print("Can you answer three questions and score maximum points?")
  28.     fa.write(str(score))
  29.     fh.write(str(highscore))  # this overwrites the initial 0 with nothing ?
  30.     print("files written #0")
  31.  
  32.     print("Question 1: What is the product of 2x2x2?")
  33.     answer = int(input("Your answer :>> "))
  34.     if answer == 8:
  35.         print("Correct")
  36.         score = score + 1
  37.         print("For Question 1 you earned 1 for a score of:", score)
  38.         fa.write(str(score) + "\n")
  39.         fh.write(str(highscore))
  40.     else:
  41.         print("Incorrect, the answer to Question 1 is 8")
  42.  
  43.     #  break
  44.  
  45.     print("Question 2: What is the product of 3x3 ?")
  46.     answer = int(input("Your answer :>> "))
  47.     if answer == 9:
  48.         print("Correct")
  49.         score = score + 1
  50.         print("For Question 2 you earned another 1 for a score of:", score)
  51.         fa.write(str(score) + "\n")
  52.         fh.write(str(highscore))
  53.  
  54.     else:
  55.         print("Incorrect, the answer is 9 ")
  56.        
  57.     #  break
  58.  
  59.     print("Question 3: What is the product of 5x2 ?")
  60.     #  answer = int(input("Your answer :>> "))
  61.     answer = input("Your answer :>> ")
  62.     if answer == "10":
  63.         print("Correct")
  64.         score = score + 1
  65.         print("For Question 3 you earned another 1 for a score of", score)
  66.         fa.write(str(score) + "\n")
  67.         fh.write(str(highscore))
  68.     else:
  69.         print("Incorrect, the answer is 10")
  70.  
  71.     print("Your score is", score)
  72.  
  73.     if score >= highscore:
  74.         highscore = score
  75.         print("You have set a new highscore of:", highscore)
  76.         with open("1_10_highscore.txt", "w") as fh:
  77.             fh.write(str(highscore))
  78.         #  with open("1_10_score.txt", "a") as fa:
  79.         #      fa.write(str(score) + "\n")
  80.     else:
  81.         print("Better luck next time")
  82.    
  83.     print("The highscore is", highscore)
  84.    
  85.     fa.close()
  86.     fh.close()
  87.    
  88.     break
  89.  
  90. # Add more questions. ... OK
  91. # "Question 1: What is the product of 2x2x2 ?", 8
  92. # "Question 2: What is the product of 3x3 ?", 9
  93. # "Question 3: What is the product of 5x2 ?", 10
  94.  
  95. #  Save running score .. OK.
  96. #  Save high score ... unsatisfactory.
  97.  
  98. # Handle the incorrect responses to the questions.
  99.  
  100. # now 1_10_highscore.txt was "0" looks like this "3000"
  101.  
Add Comment
Please, Sign In to add comment