-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess_game.py
More file actions
60 lines (48 loc) · 2.02 KB
/
guess_game.py
File metadata and controls
60 lines (48 loc) · 2.02 KB
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
import random
import score
class Guess_Game:
"""
The purpose of guess game is to start a new game, cast a random number between 1 to a
variable called difficulty . The game will get a number input from the Properties:
1. Difficulty
2. Secret number
Methods
1. generate_number - Will generate number between 1 to difficulty and save it to
secret_number.
2. get_guess_from_user - Will prompt the user for a number between 1 to difficulty and
return the number.
3. compare_results - Will compare the the secret generated number to the one prompted
by the get_guess_from_user.
4. play - Will call the functions above and play the game. Will return True / False if the user
lost or won.
"""
def __init__(self, difficulty) -> None:
self.difficulty = int(difficulty)
self.match = False
def generate_number(self):
self.secret_number = random.randrange(1, self.difficulty + 1)
print(f"Generate a secret number equals to: {self.secret_number}")
def get_guess_from_user(self):
# generate number between 1 to difficulty and save it to secret_number.
self.user_guess = int(input("PLease enter your guessed number: "))
print(f"Got the number {self.user_guess} from the user")
def compare_results(self):
print(f"Comparing secret number {self.secret_number} with user input {self.user_guess}")
if self.secret_number == self.user_guess:
self.match = True
def play(self):
self.generate_number()
self.get_guess_from_user()
self.compare_results()
if self.match == True:
print("\n========================================")
print(f"We got a winner!!!\n Our secret number {self.secret_number} is equal to your guessed number {self.user_guess}")
print("========================================\n")
score.main(self.difficulty)
else:
print("Nop, you missed it. This is not the chosen number")
def main(difficulty):
game1 = Guess_Game(difficulty)
game1.play()
if __name__ == "__main__":
main()