-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path25_practice.py
More file actions
30 lines (24 loc) · 804 Bytes
/
Copy path25_practice.py
File metadata and controls
30 lines (24 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#Write a program to print the game()function lets a user play a game and return the score as integer .
# You need to read a file 'Hi-score.txt'which is either blank or contain to update Hi-score when game()function breaks Hi score
import random
def game():
print("You are playing the game...")
score = random.randint(1, 62)
return score
# Read previous hi-score
try:
with open("hiscore.txt", "r") as f:
hiscore = f.read().strip()
hiscore = int(hiscore) if hiscore else 0
except:
hiscore = 0
# Play game
score = game()
print(f"Your Score: {score}")
# Update if new score is better
if score > hiscore:
print("Congratulations! New High Score!")
with open("hiscore.txt", "w") as f:
f.write(str(score))
else:
print("High Score remains:", hiscore)