-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_game_version2.py
52 lines (46 loc) · 1.07 KB
/
number_game_version2.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
import random
#prompt to start game
#Player picks a number between 1 and 10
#Computer guesses until it finds the right number
#new game prompt
def new_game():
new_game_prompt = input("Play again? Y/n?").lower()
if new_game_prompt == 'n':
print("Bye!")
exit()
elif new_game_prompt == 'y':
game()
new_game()
else:
print("Error. Please restart game")
exit()
def game():
answer = input("Enter a number between 1 and 10: ")
answer = int(answer)
count = 0
min_guess = 1
max_guess = 10
guess_count = 0
while True:
guess = random.randint(min_guess, max_guess)
guess_count += 1
if guess < answer:
print("Too low. Try Again.")
min_guess = guess
continue
elif guess > answer:
print("Too high. Try Again.")
max_guess = guess
continue
elif guess == answer:
print("Computer's guess {}. Number of guesses: {}".format(guess, guess_count))
break
else:
print("Incorrect")
if guess_count > 5:
print("Guess limit exceeded. User wins.")
break
else:
continue
game()
new_game()