Repository navigation
Expand file tree
/
Copy pathPredicting Game
More file actions
62 lines (53 loc) · 1.87 KB
/
Copy pathPredicting Game
File metadata and controls
62 lines (53 loc) · 1.87 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
59
60
61
62
# 🎯 Advanced Guessing Game Project
import random
def choose_difficulty():
"""Ask user to select a difficulty level."""
print("\nChoose difficulty level:")
print("1. Easy (10 attempts)")
print("2. Medium (7 attempts)")
print("3. Hard (5 attempts)")
while True:
choice = input("Enter choice (1-3): ").strip()
if choice == '1':
return 10
elif choice == '2':
return 7
elif choice == '3':
return 5
else:
print("❌ Invalid choice. Please select 1, 2, or 3.")
def play_game():
"""Main guessing game loop."""
num = random.randint(1, 100)
max_attempts = choose_difficulty()
attempts = 0
print("\n🎉 Welcome to the Advanced Guessing Game!")
print("I have picked a number between 1 and 100.")
print(f"You have {max_attempts} attempts. Good luck!\n")
while attempts < max_attempts:
try:
guess = int(input(f"Attempt {attempts+1}/{max_attempts} - Enter your guess: "))
attempts += 1
if guess < num:
print("📉 Too low!")
elif guess > num:
print("📈 Too high!")
else:
score = (max_attempts - attempts + 1) * 10
print(f"\n✅ Correct! The number was {num}.")
print(f"🏆 You guessed it in {attempts} attempts. Score: {score}\n")
break
except ValueError:
print("⚠️ Please enter a valid number.")
else:
print(f"\n❌ Out of attempts! The number was {num}. Better luck next time!\n")
def main():
"""Run the game with replay option."""
while True:
play_game()
again = input("Do you want to play again? (y/n): ").lower()
if again != 'y':
print("\nThanks for playing! 👋")
break
if __name__ == "__main__":
main()