-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberGuessingGame.py
More file actions
58 lines (54 loc) · 1.96 KB
/
numberGuessingGame.py
File metadata and controls
58 lines (54 loc) · 1.96 KB
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
# Init
import random
target_number = 0
guesses_left = 0
hint_text = "Unknown"
plural_corrector = ""
def start_game():
# Pregame Setup
global target_number, guesses_left, hint_text, plural_corrector
target_number = random.randint(1, 10)
guesses_left = 3
hint_text = "Unknown"
plural_corrector = ""
# Game Loop
while guesses_left > 0:
# Receive Player Input
player_input = int(input("Guess a number between 1 and 10: "))
if player_input < 1 or player_input > 10:
# Handle invalid input.
print("Invalid input. Input a number between 1 and 10.")
continue
elif player_input == target_number:
# End game on correct guess.
print("Correct.")
input("Press Enter to play again. ")
start_game()
break
else:
# Perform scripts on incorrect guess.
guesses_left -= 1
if guesses_left > 0:
# Create hint.
if player_input < target_number:
hint_text = "bigger"
else:
hint_text = "smaller"
# Correct pluralization.
if guesses_left == 1:
plural_corrector = ""
else:
plural_corrector = "es"
# Print hint and guesses left.
print("Incorrect. The number is " + hint_text + ".")
print("You have " + str(guesses_left) + " more guess" + plural_corrector + ".")
else:
# End game and reveal answer.
print("Incorrect. The correct number was " + str(target_number) + ".")
input("Press Enter to play again. ")
start_game()
# Run the game on launch.
print("Pick a number between 1-10.")
print("Use feedback to track down the number.")
print("You have 3 attempts.")
start_game()