1
+ # coding=utf-8
1
2
import random
2
3
# random module is a built-in module to generate pseudo-random variables
3
4
@@ -19,10 +20,10 @@ def startGame():
19
20
displayGameplay = display_gameplay ()
20
21
# make a list of the possible inputs
21
22
# to start or end the game
22
- POSSIBLE_RESPONSES = ['Y' ,'YES' ,'N' ,'NO' ,'EXIT' ]
23
+ possible_responses = ['Y' ,'YES' ,'N' ,'NO' ,'EXIT' ]
23
24
# get user's response
24
25
user_response = input ('\n Start game? (yes/no): ' ).strip ().upper ()
25
- while user_response not in POSSIBLE_RESPONSES :
26
+ while user_response not in possible_responses :
26
27
print ('\n Invalid Input!' )
27
28
user_response = input ('\n Start game? (yes/no): ' ).strip ().upper ()
28
29
else : return user_response
@@ -36,13 +37,15 @@ def game():
36
37
play_game = startGame ()
37
38
# assign the number of trials the user has to a variable
38
39
number_of_trials = 7
40
+ # initialise new_game to true
41
+ new_game = True
39
42
while play_game == 'YES' or play_game == 'Y' :
40
43
# make a list that contains all the
41
44
# numbers a user can guess
42
- ACCEPTED_NUMBER_PICKS = [str (i ) for i in range (1 ,11 )]
45
+ accepted_number_picks = [str (i ) for i in range (1 ,11 )]
43
46
# get user's number
44
47
user_input = input ('\n Guess a number between the range of 1-10: ' ).strip ().upper ()
45
- while user_input not in ACCEPTED_NUMBER_PICKS and user_input != 'EXIT' :
48
+ while user_input not in accepted_number_picks and user_input != 'EXIT' :
46
49
print ('Invalid Input!' )
47
50
user_input = input ('\n Guess a valid number between the range of 1-10: ' ).strip ().upper ()
48
51
if user_input == 'EXIT' :
@@ -51,7 +54,10 @@ def game():
51
54
else :
52
55
# generate a random number in the range 1-10
53
56
# and assign it to a variable
54
- computer_number = random .randint (1 ,10 )
57
+ # check if new_game, if true generate new computer_number else don't
58
+ if new_game :
59
+ computer_number = random .randint (1 ,10 )
60
+ new_game = False
55
61
user_input = int (user_input )
56
62
if user_input < computer_number :
57
63
number_of_trials -= 1
@@ -60,7 +66,7 @@ def game():
60
66
print (f'You\' ve { number_of_trials } trial(s) left' )
61
67
play_game = input ('\n Guess again? (yes/no): ' ).strip ().upper ()
62
68
else :
63
- print (F '\n Game over!, you\' ve 0 trial left..try harder next time 😉' )
69
+ print ('\n Game over!, you\' ve 0 trial left..try harder next time 😉' )
64
70
break
65
71
elif user_input > computer_number :
66
72
number_of_trials -= 1
@@ -69,14 +75,16 @@ def game():
69
75
print (f'You\' ve { number_of_trials } trial(s) left' )
70
76
play_game = input ('\n Guess again? (yes/no): ' ).strip ().upper ()
71
77
else :
72
- print (F '\n Game over!, you\' ve 0 trial left..try harder next time 😉' )
78
+ print ('\n Game over!, you\' ve 0 trial left..try harder next time 😉' )
73
79
break
74
80
elif user_input == computer_number :
75
81
number_of_trials -= 1
76
82
print (f'Congratulations!!..you guessed right, after { 7 - number_of_trials } trial(s)' )
77
83
play_game = input ('\n Do you wish to play again? (yes/no): ' ).strip ().upper ()
78
84
# if the user wishes to play again, assign
79
85
# the number of trials the user has to a variable
80
- number_of_trials = 7
86
+ number_of_trials = 7
87
+ # start a new game
88
+ new_game = True
81
89
82
90
game ()
0 commit comments