Advertisement
TonyMo

hiscore_dup_1.py

May 5th, 2021
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. # hiscore_dup_1.py
  2.  
  3. """ To duplicate runaway high score after running the script a few times.
  4. ref: Programming 103: Saving and Structuring Data
  5.    Week 1 Saving a high score
  6.  
  7. "highscore.txt" must exist in the path and contain something,
  8. initially 0 in this case. To track the highscore See comments below.
  9. Note that this erroneous code evolved due to me trying to understand how the file modes r, w, a worked.
  10. There is a curiosity in that the several integers seem to be added right to left ie the basic faulty 'number' is 3000. I would expect it to be 0003. ! """
  11.  
  12. with open("highscore.txt", "r") as fh:
  13.     # NB to read the file must have content;ie 0 (not nothing).
  14.     highscore = fh.read()  
  15.     highscore = int(highscore)
  16.     print("The highscore at start is", highscore)   # OK
  17.    
  18. fh = open("highscore.txt", "w")
  19.    
  20. # do something
  21. fh.write(str(highscore))
  22.  
  23. # do some other thing
  24. fh.write(str(highscore))
  25.  
  26. # do some more things
  27. fh.write(str(highscore))
  28.  
  29. # do some thing else
  30. fh.write(str(highscore))
  31.  
  32. # do some tidying
  33. highscore =  3
  34. with open("highscore.txt", "w") as fh:
  35.     fh.write(str(highscore))
  36.  
  37. fh.close()
  38.  
  39. """ run 1
  40. >>> %Run hiscore_dup_1.py
  41. The highscore at start is 0
  42.  
  43. run2
  44. >>> %Run hiscore_dup_1.py
  45. The highscore at start is 3000
  46.  
  47. run3
  48. >>> %Run hiscore_dup_1.py
  49. The highscore at start is 3000300030003000
  50.  
  51. run4
  52. >>>
  53. The highscore at start is 3000300030003000300030003000300030003000
  54. 300030003000300030003000
  55.  
  56. highscore.txt = 3000300030003000300030003000300030003000
  57. 3000300030003000300030003000300030003000
  58. 3000300030003000300030003000300030003000
  59. 3000300030003000300030003000300030003000
  60. 3000300030003000300030003000300030003000
  61. 30003000300030003000300030003000300030003000300030003000
  62.  
  63. """
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement