-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_guess_game.py
78 lines (60 loc) · 2.24 KB
/
final_guess_game.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
import random
PROMPT = 'What is your guess? '
QUIT = -1
QUIT_TEXT ='q'
QUIT_MESSAGE ='Thank you for playing'
CONFIRM_QUIT_MESSAGE ='Are you sure you want to quit (Y/n)? '
def confirm_quit():
"""Ask user to confirm that they want to quit
default to yes
Return True (yes, quit) or Falso (no, dont't quit) """
spam =input(CONFIRM_QUIT_MESSAGE)
if spam =='n':
return False
else:
return True
def do_guess_round():
"""Choose a random number, ask the user for a guess
check whether the guess is true
and repeat until the user is correct"""
computers_number = random.randint(1,100)
number_of_guesses = 0
while True:
players_guess =input(PROMPT)
if players_guess == QUIT_TEXT:
if confirm_quit():
return QUIT
else:
continue
number_of_guesses = number_of_guesses+1
if computers_number ==int(players_guess):
print('Correct!')
return number_of_guesses
elif computers_number >int(players_guess):
print('Too Low')
else:
print('Too High')
total_rounds = 0
total_guesses = 0
while True:
total_rounds = total_rounds+1
print("Starting round number: "+str(total_rounds))
print("Let the guessing begin!!!")
this_round = do_guess_round()
if this_round == QUIT:
total_rounds = total_rounds - 1
avg = str(total_guesses/float(total_rounds))
if total_rounds == 0:
stats_message = 'You completed no rounds. '+\
'Please try again later.'
else:
stats_message = 'You played ' + str(total_rounds) +\
' rounds, with an average of '+\
str(avg)
break
total_guesses = total_guesses+this_round
avg =str(total_guesses/float (total_rounds))
print("You took "+str(this_round)+" guesses")
print("Your guessing average = "+avg)
print("")
print(stats_message)