diff --git a/1-Number_Guessing_Game.py b/1-Number_Guessing_Game.py index 1d04694..fc5f197 100644 --- a/1-Number_Guessing_Game.py +++ b/1-Number_Guessing_Game.py @@ -1,45 +1,55 @@ import random + def number_guessing_game(): print("Welcome to the Number Guessing Game!") - print("I'm thinking of a number between 1 and 10...") - - # Generate a random number between 1 and 10 - secret_number = random.randint(1, 10) - - # Maximum number of attempts - max_attempts = 3 - attempts = 0 - - # Start the game loop - while attempts < max_attempts: - try: - # Get user's guess - guess = int(input(" Enter your guess: ")) - - # Check if the guess is correct - if guess == secret_number: - print(f"Congratulations! You guessed the number {secret_number} correctly!") - break - else: - print("Sorry, Try again.") - - # Increase the number of attempts - attempts += 1 - - except ValueError: - print("Invalid input! Please enter a valid number.") - - # If max attempts reached and no correct guess - if attempts == max_attempts and guess != secret_number: - print(f"Game over! The correct number was {secret_number}.") - - # Ask to play again - play_again = input("\n Do you want to play again? (yes/no): ").lower() - if play_again == "yes": - number_guessing_game() - else: - print("Thanks for playing! Goodbye.") - -# Run the game + + while True: + print("\nI'm thinking of a number between 1 and 10...") + + secret_number = random.randint(1, 10) + max_attempts = 3 + attempts = 0 + + while attempts < max_attempts: + try: + remaining_attempts = max_attempts - attempts + print(f"Attempts remaining: {remaining_attempts}") + + guess = int(input("Enter your guess: ")) + + if guess < 1 or guess > 10: + print("Please enter a number between 1 and 10.") + continue + + if guess == secret_number: + print( + f"Congratulations! You guessed the number " + f"{secret_number} correctly!" + ) + break + + elif guess < secret_number: + print("Too low! Try again.") + + else: + print("Too high! Try again.") + + attempts += 1 + + except ValueError: + print("Invalid input! Please enter a valid number.") + + if attempts == max_attempts and guess != secret_number: + print(f"Game over! The correct number was {secret_number}.") + + play_again = input( + "\nDo you want to play again? (yes/no): " + ).lower() + + if play_again != "yes": + print("Thanks for playing! Goodbye.") + break + + number_guessing_game() diff --git a/2-To-do-List.py b/2-To-do-List.py index 65effa9..f3bcd87 100644 --- a/2-To-do-List.py +++ b/2-To-do-List.py @@ -27,13 +27,18 @@ elif choice == "3": if not tasks: print("No tasks to delete!") - else: - task_number = int(input("🗑️ Enter task number to delete: ")) - if 1 <= task_number <= len(tasks): - deleted_task = tasks.pop(task_number - 1) - print(f"Task deleted: {deleted_task}") - else: - print("Invalid task number!") + else: + try: + task_number= int(input("🗑️ Enter task number to delete:")) + + if 1 <= task_number <=len(tasks): + deleted_task= tasks.pop(task_number - 1) + print(f"Task deleted: {deleted_task}") + else: + print("Invalid task number!") + + except ValueError: + print("Invalid input! Please enter a task number.") elif choice == "4": print("Exiting... Goodbye!")