Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # Function that lets the player choose difficulty level
- def choose_difficulty():
- print("🎮 Choose a difficulty level:")
- print("1 - Easy (1–10)")
- print("2 - Normal (1–50)")
- print("3 - Hard (1–100)")
- while True:
- choice = input("Your choice (1/2/3): ")
- if choice == '1':
- return 1, 10
- elif choice == '2':
- return 1, 50
- elif choice == '3':
- return 1, 100
- else:
- print("Invalid choice. Please try again.")
- # Helper function that checks if the input is a valid integer
- def is_valid_integer(text):
- return text.isdigit()
- # Main game logic
- def play_game():
- low, high = choose_difficulty()
- secret = random.randint(low, high) # Random number to guess
- attempts = 0
- previous_diff = None # Distance between previous guess and secret number
- print(f"\n🔢 Guess the number between {low} and {high}!")
- while True:
- user_input = input("Your guess: ")
- if not is_valid_integer(user_input):
- print("⚠️ Please enter a valid integer!")
- continue
- guess = int(user_input)
- if guess < low or guess > high:
- print(f"🚫 Number out of range! Enter a number between {low} and {high}.")
- continue
- attempts += 1
- current_diff = abs(secret - guess)
- if guess == secret:
- print(f"🎉 Congratulations! You guessed it in {attempts} tries.")
- return attempts
- # First attempt hint
- if previous_diff is None:
- if current_diff > (high - low) // 2:
- print("🥶 Cold...")
- else:
- print("🌡️ Warm!")
- else:
- # Feedback based on distance from the previous guess
- if current_diff < previous_diff:
- print("🔥 Warmer!")
- elif current_diff > previous_diff:
- print("❄️ Colder!")
- else:
- print("😐 Same as last time...")
- previous_diff = current_diff
- # Game loop and result summary
- def main():
- results = []
- print("🎯 Welcome to the game: Hot-Cold – Guess the Number!")
- while True:
- attempts = play_game()
- results.append(attempts)
- again = input("\nDo you want to play again? (y/n): ").lower()
- if again != 'y':
- break
- print("\n📊 Your results:")
- for i, tries in enumerate(results, 1):
- print(f"Round {i}: {tries} attempts")
- print(f"🔚 Average number of attempts: {sum(results) / len(results):.2f}")
- # Run the game if this file is executed directly
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment