1
+ import random
2
+ # random module is a built-in module to generate pseudo-random variables
3
+
4
+ def display_gameplay ():
5
+ """
6
+ Displays the gameplay
7
+ : return: None
8
+ """
9
+ print ('\n Welcome to Number Guessing Game!' )
10
+ print ('In this game you\' ve just 7 trials to guess a number between the range of 1-10' )
11
+ print ('Note: enter \' exit\' to end game' )
12
+
13
+ def startGame ():
14
+ """
15
+ Gets user response to start or end the game
16
+ : return: str
17
+ """
18
+ # call the function to display gameplay
19
+ displayGameplay = display_gameplay ()
20
+ # make a list of the possible inputs
21
+ # to start or end the game
22
+ POSSIBLE_RESPONSES = ['Y' ,'YES' ,'N' ,'NO' ,'EXIT' ]
23
+ # get user's response
24
+ user_response = input ('\n Start game? (yes/no): ' ).strip ().upper ()
25
+ while user_response not in POSSIBLE_RESPONSES :
26
+ print ('\n Invalid Input!' )
27
+ user_response = input ('\n Start game? (yes/no): ' ).strip ().upper ()
28
+ else : return user_response
29
+
30
+ def game ():
31
+ """
32
+ Controls the game
33
+ : return: None
34
+ """
35
+ # call the function to get user's response
36
+ play_game = startGame ()
37
+ # assign the number of trials the user has to a variable
38
+ number_of_trials = 7
39
+ while play_game == 'YES' or play_game == 'Y' :
40
+ # make a list that contains all the
41
+ # numbers a user can guess
42
+ ACCEPTED_NUMBER_PICKS = [str (i ) for i in range (1 ,11 )]
43
+ # get user's number
44
+ 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' :
46
+ print ('Invalid Input!' )
47
+ user_input = input ('\n Guess a valid number between the range of 1-10: ' ).strip ().upper ()
48
+ if user_input == 'EXIT' :
49
+ print ('Bye Player!' )
50
+ break
51
+ else :
52
+ # generate a random number in the range 1-10
53
+ # and assign it to a variable
54
+ computer_number = random .randint (1 ,10 )
55
+ user_input = int (user_input )
56
+ if user_input < computer_number :
57
+ number_of_trials -= 1
58
+ print (f'Oops, { user_input } is too low' )
59
+ if number_of_trials != 0 :
60
+ print (f'You\' ve { number_of_trials } trial(s) left' )
61
+ play_game = input ('\n Guess again? (yes/no): ' ).strip ().upper ()
62
+ else :
63
+ print (F'\n Game over!, you\' ve 0 trial left..try harder next time 😉' )
64
+ break
65
+ elif user_input > computer_number :
66
+ number_of_trials -= 1
67
+ print (f'Oops, { user_input } is too high' )
68
+ if number_of_trials != 0 :
69
+ print (f'You\' ve { number_of_trials } trial(s) left' )
70
+ play_game = input ('\n Guess again? (yes/no): ' ).strip ().upper ()
71
+ else :
72
+ print (F'\n Game over!, you\' ve 0 trial left..try harder next time 😉' )
73
+ break
74
+ elif user_input == computer_number :
75
+ number_of_trials -= 1
76
+ print (f'Congratulations!!..you guessed right, after { 7 - number_of_trials } trial(s)' )
77
+ play_game = input ('\n Do you wish to play again? (yes/no): ' ).strip ().upper ()
78
+ # if the user wishes to play again, assign
79
+ # the number of trials the user has to a variable
80
+ number_of_trials = 7
81
+
82
+ game ()
0 commit comments