Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,276 questions

52,302 answers

573 users

How to write the 7 Boom game. If a number is divided by 7 or includes the digit 7, the user writes Boom in Python

1 Answer

0 votes
# 7 Boom game. The user enters a number he wants to start the game, 
# (-1 to end the game, or lost), then the user enters numbers, from the number he entered
# If the current number is divisible by 7, or one of its digits is 7, 
# And the user did not write the word 'Boom', the user loses. 

def contains_seven(num: int) -> bool:
    """Check if the number contains the digit 7."""
    return "7" in str(num)


def is_boom(num: int) -> bool:
    """Check if the number is divisible by 7 or contains 7."""
    return num % 7 == 0 or contains_seven(num)


def get_user_input() -> str:
    """Ask the user for input."""
    return input("Enter a number (or Boom): ").strip()


def process_turn(expected_number: int, user_input: str) -> bool:
    """Validate the user's move. Return False if the user loses."""
    should_boom = is_boom(expected_number)
    lower = user_input.lower()

    if should_boom:
        if lower != "boom":
            print(f"{user_input} (should be Boom)")
            print("YOU LOSE!")
            return False
    else:
        if lower == "boom":
            print(f"Wrong move! It was {expected_number}")
            print("YOU LOSE!")
            return False

        # Must be the correct number
        if not user_input.isdigit() or int(user_input) != expected_number:
            print(f"Wrong move! It was {expected_number}")
            print("YOU LOSE!")
            return False

    return True


def run_game():
    """Main game loop."""
    start_input = input("Enter a number from which you want to start the game (-1 to exit): ").strip()

    if not start_input.isdigit() and start_input != "-1":
        return

    current_num = int(start_input)
    if current_num == -1:
        return

    while True:
        user_input = get_user_input()

        if user_input == "-1":
            print("Game Over!")
            break

        if not process_turn(current_num, user_input):
            break

        current_num += 1


if __name__ == "__main__":
    run_game()



'''
run:

Enter a number from which you want to start the game (-1 to exit): 6
Enter a number (or Boom): 6
Enter a number (or Boom): Boom
Enter a number (or Boom): 8
Enter a number (or Boom): 9
Enter a number (or Boom): 10
Enter a number (or Boom): 11
Enter a number (or Boom): 12
Enter a number (or Boom): 13
Enter a number (or Boom): Boom
Enter a number (or Boom): 15
Enter a number (or Boom): 16
Enter a number (or Boom): Boom
Enter a number (or Boom): 18
Enter a number (or Boom): 19
Enter a number (or Boom): 20
Enter a number (or Boom): 21
21 (should be Boom)
YOU LOSE!

'''

 



answered 1 hour ago by avibootz
...