|
| 1 | +import random |
| 2 | + |
| 3 | +def generate_puzzle(difficulty): |
| 4 | + if difficulty == "easy": |
| 5 | + return generate_easy_puzzle() |
| 6 | + elif difficulty == "medium": |
| 7 | + return generate_medium_puzzle() |
| 8 | + elif difficulty == "hard": |
| 9 | + return generate_hard_puzzle() |
| 10 | + else: |
| 11 | + raise ValueError("Invalid difficulty level. Please choose 'easy', 'medium', or 'hard'.") |
| 12 | + |
| 13 | +def generate_easy_puzzle(): |
| 14 | + puzzle_type = random.choice(["arithmetic", "string"]) |
| 15 | + if puzzle_type == "arithmetic": |
| 16 | + num1 = random.randint(1, 10) |
| 17 | + num2 = random.randint(1, 10) |
| 18 | + operator = random.choice(["+", "-"]) |
| 19 | + puzzle = f"{num1} {operator} {num2}" |
| 20 | + solution = eval(puzzle) |
| 21 | + hint = "This is an arithmetic puzzle." |
| 22 | + else: # puzzle_type == "string" |
| 23 | + word = random.choice(["hello", "python", "coding", "challenge"]) |
| 24 | + shuffle_word = "".join(random.sample(word, len(word))) |
| 25 | + puzzle = shuffle_word |
| 26 | + solution = word |
| 27 | + hint = "Rearrange the letters to form a meaningful word." |
| 28 | + |
| 29 | + return puzzle, solution, hint |
| 30 | + |
| 31 | +def generate_medium_puzzle(): |
| 32 | + puzzle_type = random.choice(["arithmetic", "string", "logical"]) |
| 33 | + if puzzle_type == "arithmetic": |
| 34 | + num1 = random.randint(10, 50) |
| 35 | + num2 = random.randint(1, 10) |
| 36 | + operator = random.choice(["+", "-", "*"]) |
| 37 | + puzzle = f"{num1} {operator} {num2}" |
| 38 | + solution = eval(puzzle) |
| 39 | + hint = "This is an arithmetic puzzle." |
| 40 | + elif puzzle_type == "string": |
| 41 | + word = random.choice(["apple", "banana", "orange", "grape"]) |
| 42 | + num_chars_to_remove = random.randint(1, len(word) - 1) |
| 43 | + indices_to_remove = random.sample(range(len(word)), num_chars_to_remove) |
| 44 | + puzzle = "".join(c if i not in indices_to_remove else "_" for i, c in enumerate(word)) |
| 45 | + solution = word |
| 46 | + hint = f"Remove {num_chars_to_remove} letter(s) to reveal the original word." |
| 47 | + else: # puzzle_type == "logical" |
| 48 | + num1 = random.randint(1, 10) |
| 49 | + num2 = random.randint(1, 10) |
| 50 | + operator = random.choice(["and", "or"]) |
| 51 | + puzzle = f"{num1} {operator} {num2}" |
| 52 | + solution = eval(puzzle.capitalize()) |
| 53 | + hint = "This is a logical puzzle." |
| 54 | + |
| 55 | + return puzzle, solution, hint |
| 56 | + |
| 57 | +def generate_hard_puzzle(): |
| 58 | + puzzle_type = random.choice(["arithmetic", "string", "logical"]) |
| 59 | + if puzzle_type == "arithmetic": |
| 60 | + num1 = random.randint(50, 100) |
| 61 | + num2 = random.randint(10, 20) |
| 62 | + operator = random.choice(["+", "-", "*", "/"]) |
| 63 | + puzzle = f"{num1} {operator} {num2}" |
| 64 | + solution = eval(puzzle) |
| 65 | + hint = "This is an arithmetic puzzle." |
| 66 | + elif puzzle_type == "string": |
| 67 | + word = random.choice(["python", "programming", "challenge"]) |
| 68 | + num_chars_to_replace = random.randint(1, len(word) - 1) |
| 69 | + indices_to_replace = random.sample(range(len(word)), num_chars_to_replace) |
| 70 | + replacement_chars = "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=num_chars_to_replace)) |
| 71 | + puzzle = "".join(c if i not in indices_to_replace else replacement_chars[idx] |
| 72 | + for idx, c in enumerate(word)) |
| 73 | + solution = word |
| 74 | + hint = f"Replace {num_chars_to_replace} letter(s) with {replacement_chars} to reveal the original word." |
| 75 | + else: # puzzle_type == "logical" |
| 76 | + num1 = random.randint(1, 10) |
| 77 | + num2 = random.randint(1, 10) |
| 78 | + operator = random.choice(["and", "or", "not"]) |
| 79 | + puzzle = f"{operator.capitalize()} {num1} == {num2}" |
| 80 | + solution = eval(f"{num1} {operator} {num2}") |
| 81 | + hint = "This is a logical puzzle." |
| 82 | + |
| 83 | + return puzzle, solution, hint |
| 84 | + |
| 85 | +def main(): |
| 86 | + print("Welcome to the Coding Puzzle Generator!") |
| 87 | + difficulty_level = input("Choose difficulty level (easy, medium, hard): ").lower() |
| 88 | + |
| 89 | + try: |
| 90 | + puzzle, solution, hint = generate_puzzle(difficulty_level) |
| 91 | + print("\nSolve the puzzle:") |
| 92 | + print(f"Question: {puzzle}") |
| 93 | + |
| 94 | + attempts = 3 |
| 95 | + while attempts > 0: |
| 96 | + user_answer = input("Your answer: ").strip() |
| 97 | + if user_answer.lower() == "hint": |
| 98 | + print(f"HINT: {hint}") |
| 99 | + else: |
| 100 | + try: |
| 101 | + user_answer = float(user_answer) # Convert to float for numeric puzzles |
| 102 | + if user_answer == solution: |
| 103 | + print("Congratulations! Your answer is correct.") |
| 104 | + break |
| 105 | + else: |
| 106 | + attempts -= 1 |
| 107 | + if attempts > 0: |
| 108 | + print(f"Sorry, that's incorrect. You have {attempts} {'attempts' if attempts > 1 else 'attempt'} remaining.") |
| 109 | + else: |
| 110 | + print(f"Sorry, the correct answer is: {solution}") |
| 111 | + except ValueError: |
| 112 | + print("Invalid input. Please enter a numeric answer.") |
| 113 | + except ValueError as e: |
| 114 | + print(e) |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments