-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (55 loc) · 1.75 KB
/
main.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import classes
import os
import json
'''
This is a game where the user must guess a number from 1 - 100. The closer the user is to the number, the more points the user earns.
Point values are stored alongside a username in an SQL database of Dict data type variables. This is called after the user logs into the game at the start of the program.
'''
def select_user(username):
if not os.path.exists("users.json"):
file_content = {
username: {
"username": username,
"score": 0
}
}
json_file = open("users.json", "w")
json_file.write(json.dumps(file_content, indent=2))
json_file.close()
users_file = open("users.json")
users_dict = json.loads(users_file.read())
users_file.close()
if not username in users_dict.keys():
users_dict[username] = {
"username": username,
"score": 0
}
users_file = open("users.json", "w")
users_file.write(json.dumps(users_dict, indent=2))
users_file.close()
user_data = users_dict.get(username)
player = classes.User(user_data["username"], user_data["score"])
return player
def score_changer(player: object):
users_file = open("users.json")
users_dict = json.loads(users_file.read())
users_file.close()
users_dict[player.name] = {
"username": player.name,
"score": player.score
}
users_file = open("users.json", "w")
users_file.write(json.dumps(users_dict, indent=2))
users_file.close()
def main(username):
player = select_user(username)
while True:
print("\n--------------------\n")
print(player)
score_change = input("\nScore change: ")
try:
player.score += int(score_change)
except ValueError:
print("\n\nEnter a valid whole number.")
score_changer(player)
main(input("Username: "))