Skip to content

Commit f303b92

Browse files
authored
Create Number Guessing Game.md
1 parent 5c7113f commit f303b92

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

To-do List/Number Guessing Game.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random #importing the random module
2+
3+
def main(): #main function
4+
print("🎲 Welcome to the Number Guessing Game!")
5+
print("I'm thinking of a number between 1 and 100.")
6+
print("Try to guess it in as few attempts as possible.\n")
7+
8+
# Generate a random number between 1 and 100
9+
number_to_guess = random.randint(1, 100)
10+
attempts = 0
11+
12+
while True:
13+
try:
14+
guess = int(input("Enter your guess: "))
15+
attempts += 1
16+
17+
if guess < number_to_guess:
18+
print("Too low! Try again.")
19+
elif guess > number_to_guess:
20+
print("Too high! Try again.")
21+
else:
22+
print(f"🎉 Correct! You guessed the number in {attempts} attempts.")
23+
break
24+
25+
except ValueError:
26+
print("⚠️ Please enter a valid number.")
27+
28+
if __name__ == "__main__":
29+
main()

0 commit comments

Comments
 (0)