-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer_guessing_game.py
71 lines (52 loc) · 2.33 KB
/
computer_guessing_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
import random
import sys
def computer_guess_number():
# Ask user to think of a secret number.
print("\nThink of a number between 1 and 100 .. and I will try to guess it.\n")
input("Press Enter when you're ready...\n")
# Initialize a counter for attempts
computer_attempts = 0
# Initialize game limits
low = 1
high = 100
while True:
# Let the computer make a random guess.
computer_guess = random.randint(low, high)
print(f"My guess is: {computer_guess}")
# Increment and display computer attempts each round.
computer_attempts += 1
print(f"\nAttempt #{computer_attempts}\n")
# Get Feedback from user.
feedback = input(f"Is {computer_guess} Correct (C), Too Low (L), or Too High (H)?\n").strip().lower()
# Validate the feedback
while feedback not in ["c", "l", "h"]:
print("\nYou must enter C, L or H.")
feedback = input(f"Is {computer_guess} Correct (C), Too Low (L), or Too High (H)?\n").strip().lower()
# Adjust the range based on the feedback
if feedback == "l":
low = computer_guess + 1
elif feedback == "h":
high = computer_guess - 1
else:
print(f"\n🎉🎉 Yey! I guessed your number correctly in only {computer_attempts} attempts!!")
break
# Check for conflicts in the range
if low > high:
print("\nOops! Something went wrong with your inputs. The range is invalid.")
print("Let's try again, but please be careful with your responses.")
low, high = 1, 100 # Reset the range
# Ask user if they want to play again.
print("\nPlay again?!")
playagain = input("Y for Yes\nQ to Quit\n").strip().lower()
# Validate play again input.
while playagain not in ["y", "q"]:
playagain = input("Y for Yes\nQ to Quit\n").strip().lower()
if playagain == "y":
print("===== Welcome Back 😍 =====")
return computer_guess_number()
if playagain == "q":
print("\n🎉🎉🎉🎉\nThank you for playing\nBye 👋")
sys.exit()
if __name__ == '__main__':
print("===== Welcome To The Guess Number Game 🎲 =====")
computer_guess_number()