# 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!
'''