coding_giants

hot-cold-starter-cg

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