-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNumber Guessing Game.py
More file actions
55 lines (43 loc) · 1.47 KB
/
Number Guessing Game.py
File metadata and controls
55 lines (43 loc) · 1.47 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
# number-guessing-game.py
import random
def play_game():
# Initialize counters for high, low, and win
high = 0
low = 0
win = 0
# Generate a random number between 1 and 100
number = random.randint(1, 100)
while win == 0:
try:
# Ask the user to input a guess
userNum = int(input("Please guess a number between 1 and 100: "))
# Check if the user's guess is too high, correct, or too low
if userNum > number:
message = "Too high, try again."
high += 1
elif userNum == number:
message = "You got it correct! Congratulations!"
win += 1
else:
message = "Too low, try again."
low += 1
# Print the appropriate message
print()
print(message)
except ValueError:
print("\nInvalid input. Please enter a number.\n")
# Display the total number of guesses
print()
print("Number of times too high:", high)
print("Number of times too low:", low)
print("Total number of guesses:", high + low + win)
# -------- Play Again Feature --------
def main():
while True:
play_game()
choice = input("\nDo you want to play again? (y/n): ").lower()
if choice != 'y':
print("\nThanks for playing! 👋")
break
# Start the game
main()