-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNumber Guessing Game.py
40 lines (32 loc) · 1.06 KB
/
Number 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
# number-guessing-game.py
import random
def main():
# 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:
# 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)
# 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)
# Call the main function to start the game
main()