coding_giants

hot-cold-cg

Aug 31st, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. # Function that lets the player choose difficulty level
  4. def choose_difficulty():
  5.     print("🎮 Choose a difficulty level:")
  6.     print("1 - Easy (1–10)")
  7.     print("2 - Normal (1–50)")
  8.     print("3 - Hard (1–100)")
  9.     while True:
  10.         choice = input("Your choice (1/2/3): ")
  11.         if choice == '1':
  12.             return 1, 10
  13.         elif choice == '2':
  14.             return 1, 50
  15.         elif choice == '3':
  16.             return 1, 100
  17.         else:
  18.             print("Invalid choice. Please try again.")
  19.  
  20. # Helper function that checks if the input is a valid integer
  21. def is_valid_integer(text):
  22.     return text.isdigit()
  23.  
  24. # Main game logic
  25. def play_game():
  26.     low, high = choose_difficulty()
  27.     secret = random.randint(low, high)  # Random number to guess
  28.     attempts = 0
  29.     previous_diff = None  # Distance between previous guess and secret number
  30.  
  31.     print(f"\n🔢 Guess the number between {low} and {high}!")
  32.  
  33.     while True:
  34.         user_input = input("Your guess: ")
  35.         if not is_valid_integer(user_input):
  36.             print("⚠️ Please enter a valid integer!")
  37.             continue
  38.  
  39.         guess = int(user_input)
  40.         if guess < low or guess > high:
  41.             print(f"🚫 Number out of range! Enter a number between {low} and {high}.")
  42.             continue
  43.  
  44.         attempts += 1
  45.         current_diff = abs(secret - guess)
  46.  
  47.         if guess == secret:
  48.             print(f"🎉 Congratulations! You guessed it in {attempts} tries.")
  49.             return attempts
  50.  
  51.         # First attempt hint
  52.         if previous_diff is None:
  53.             if current_diff > (high - low) // 2:
  54.                 print("🥶 Cold...")
  55.             else:
  56.                 print("🌡️ Warm!")
  57.         else:
  58.             # Feedback based on distance from the previous guess
  59.             if current_diff < previous_diff:
  60.                 print("🔥 Warmer!")
  61.             elif current_diff > previous_diff:
  62.                 print("❄️ Colder!")
  63.             else:
  64.                 print("😐 Same as last time...")
  65.  
  66.         previous_diff = current_diff
  67.  
  68. # Game loop and result summary
  69. def main():
  70.     results = []
  71.  
  72.     print("🎯 Welcome to the game: Hot-Cold – Guess the Number!")
  73.  
  74.     while True:
  75.         attempts = play_game()
  76.         results.append(attempts)
  77.  
  78.         again = input("\nDo you want to play again? (y/n): ").lower()
  79.         if again != 'y':
  80.             break
  81.  
  82.     print("\n📊 Your results:")
  83.     for i, tries in enumerate(results, 1):
  84.         print(f"Round {i}: {tries} attempts")
  85.     print(f"🔚 Average number of attempts: {sum(results) / len(results):.2f}")
  86.  
  87. # Run the game if this file is executed directly
  88. if __name__ == "__main__":
  89.     main()
  90.  
Add Comment
Please, Sign In to add comment