Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # hiscore_dup_1.py
- """ To duplicate runaway high score after running the script a few times.
- ref: Programming 103: Saving and Structuring Data
- Week 1 Saving a high score
- "highscore.txt" must exist in the path and contain something,
- initially 0 in this case. To track the highscore See comments below.
- Note that this erroneous code evolved due to me trying to understand how the file modes r, w, a worked.
- 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. ! """
- with open("highscore.txt", "r") as fh:
- # NB to read the file must have content;ie 0 (not nothing).
- highscore = fh.read()
- highscore = int(highscore)
- print("The highscore at start is", highscore) # OK
- fh = open("highscore.txt", "w")
- # do something
- fh.write(str(highscore))
- # do some other thing
- fh.write(str(highscore))
- # do some more things
- fh.write(str(highscore))
- # do some thing else
- fh.write(str(highscore))
- # do some tidying
- highscore = 3
- with open("highscore.txt", "w") as fh:
- fh.write(str(highscore))
- fh.close()
- """ run 1
- >>> %Run hiscore_dup_1.py
- The highscore at start is 0
- run2
- >>> %Run hiscore_dup_1.py
- The highscore at start is 3000
- run3
- >>> %Run hiscore_dup_1.py
- The highscore at start is 3000300030003000
- run4
- >>>
- The highscore at start is 3000300030003000300030003000300030003000
- 300030003000300030003000
- highscore.txt = 3000300030003000300030003000300030003000
- 3000300030003000300030003000300030003000
- 3000300030003000300030003000300030003000
- 3000300030003000300030003000300030003000
- 3000300030003000300030003000300030003000
- 30003000300030003000300030003000300030003000300030003000
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement