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