



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Best python practicel file. B.
Typology: Schemes and Mind Maps
1 / 5
This page cannot be seen from the preview
Don't miss anything!




import random import sys def print_welcome(): print("=" * 50) print(" WELCOME TO NUMBER GUESSING GAME") print("=" * 50) print("Rules:") print("- I will think of a number in a certain range.") print("- You have limited attempts based on difficulty.") print("- After each guess, I will say Too High / Too Low.") print("- Try to guess in minimum attempts.") def choose_difficulty(): print("Choose difficulty level:") print("1. Easy (Number between 1 and 20, 10 attempts)") print("2. Medium (Number between 1 and 50, 7 attempts)") print("3. Hard (Number between 1 and 100, 5 attempts)") while True: choice = input("Enter 1, 2 or 3: ").strip() if choice == "1":
return 1, 20, 10 elif choice == "2": return 1, 50, 7 elif choice == "3": return 1, 100, 5 else: print("Invalid choice. Please enter 1, 2 or 3.") def get_guess(lower, upper): while True: guess_str = input(f"Enter your guess ({lower}-{upper}): ").strip() try: guess = int(guess_str) if guess < lower or guess > upper: print(f"Please enter a number between {lower} and {upper}.") else: return guess except ValueError: print("Invalid input! Please enter a valid integer.") def play_round(): lower, upper, max_attempts = choose_difficulty()
print("HINT: The secret number is EVEN.") else: print("HINT: The secret number is ODD.") print(f"Out of attempts! The correct number was {secret_number}.") return None def ask_play_again(): while True: choice = input("Do you want to play again? (y/n): ").strip().lower() if choice in ("y", "yes"): return True elif choice in ("n", "no"): return False else: print("Please enter 'y' or 'n'.") def main(): print_welcome() best_score = None while True:
result = play_round() if result is not None: if best_score is None or result < best_score: best_score = result print(f"New best score: {best_score} attempts!") else: print(f"Best score so far: {best_score} attempts.") else: if best_score is not None: print(f"Best score so far: {best_score} attempts.") else: print("No best score yet. Try to win a round!") if not ask_play_again(): print("Thanks for playing! Goodbye!") sys.exit() if name == "main": main()