-
Notifications
You must be signed in to change notification settings - Fork 59
/
secret_number.py
127 lines (119 loc) · 4.79 KB
/
secret_number.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
##############################################
# The Secret Number Guessing Game
# -------------------------------
# User must guess a number within the range of 1-100.
# Once a correct guess is recorded the program prints
# game stats which includes number of tries, max and min guess,
# summation of scores, mean, and the secret number.
# If user fails to guess the correct number within the time
# frame then the program prints the correct number and stats.
#
# Game has two modes:
# -------------------
# Mode 1: User can guess an unlimited number of times.
# Mode 2: User is given a random number of tries in the range
# of 1-10.
#
# To run the program download it to your Desktop and within the
# terminal type: python guessing_game.py
#
# By Doug Purcell
# http://www.purcellconsult.com
#
###############################################
from random import randint
option = input("Do you want unlimited tries? Enter 'y' or 'n' ")
option = option.lower()
"""
Variable Declarations:
---------------------
Defining the variables that don't
need to be reset outside of the
while loops.
"""
the_secret_number = randint(1, 100)
number_of_tries, summation, count = 0, 0, 0
max_number, min_number = the_secret_number, the_secret_number
if option == 'y':
while True:
your_guess = int(input('Enter a number in the range of 1-100 '))
if your_guess < 0 or your_guess > 100:
print('Invalid input: Must enter within the range of 1-100')
break
elif your_guess > the_secret_number:
print(your_guess, 'is bigger than the secret number ')
elif your_guess < the_secret_number:
print(your_guess, 'is less than the secret number ')
number_of_tries += 1
summation += your_guess
if your_guess > max_number:
max_number = your_guess
elif your_guess < min_number:
min_number = your_guess
elif your_guess == the_secret_number and number_of_tries == 1:
max_number = your_guess
min_number = your_guess
if your_guess == the_secret_number:
print(your_guess, 'is the correct answer')
print('Number of tries =', number_of_tries)
print('Max number you guessed =', max_number)
print('Min number you guessed =', min_number)
print('Summation of numbers =', summation)
print('The average of numbers =', round(summation / number_of_tries, 2))
break
elif option == 'n':
number_of_tries = randint(1, 10)
print('You have', number_of_tries, 'number of tries.')
while number_of_tries > 0:
your_guess = int(input('Enter a number in the range of 1-100 '))
if your_guess < 0 or your_guess > 100:
print('Invalid number: Must be in range of 1-100')
break
print(number_of_tries - 1, 'tries left!')
if your_guess > the_secret_number:
print(your_guess, 'is bigger than the secret number ')
elif your_guess < the_secret_number:
print(your_guess, 'is less than the secret number ')
count += 1
summation += your_guess
number_of_tries -= 1
"""
Takes care of the special case in which
the user only gets one chance to
guess the number.
"""
if number_of_tries == 0 and count == 1:
max_number = your_guess
min_number = your_guess
if your_guess > max_number:
max_number = your_guess
elif your_guess < min_number:
min_number = your_guess
"""
Prints the user stats.
----------------------
The first branch computes the stats if the user
guesses the correct number before number_of_tries
is equal to 0. The elif branch computes the stats if
the user doesn't guess the correct number within
the allocated number of tries.
"""
if your_guess == the_secret_number:
print(your_guess, 'is the correct answer')
print('You win!')
print('Number of tries =', count)
print('Max number you guessed =', max_number)
print('Min number you guessed =', min_number)
print('Summation of numbers =', summation)
print('The average of numbers =', round(summation / count, 2))
break
elif your_guess is not the_secret_number and number_of_tries == 0:
print('Good try :-)!')
print('Secret number is ', the_secret_number)
print('Number of tries =', count)
print('Max number you guessed =', max_number)
print('Min number you guessed =', min_number)
print('Summation of numbers =', summation)
print('The average of numbers =', round(summation / count, 2))
else:
print('Invalid option! Program terminates...')