11import random
22
3+
34def generate_puzzle (difficulty ):
45 if difficulty == "easy" :
56 return generate_easy_puzzle ()
@@ -8,7 +9,9 @@ def generate_puzzle(difficulty):
89 elif difficulty == "hard" :
910 return generate_hard_puzzle ()
1011 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+
1215
1316def generate_easy_puzzle ():
1417 puzzle_type = random .choice (["arithmetic" , "string" ])
@@ -28,6 +31,7 @@ def generate_easy_puzzle():
2831
2932 return puzzle , solution , hint
3033
34+
3135def generate_medium_puzzle ():
3236 puzzle_type = random .choice (["arithmetic" , "string" , "logical" ])
3337 if puzzle_type == "arithmetic" :
@@ -40,8 +44,10 @@ def generate_medium_puzzle():
4044 elif puzzle_type == "string" :
4145 word = random .choice (["apple" , "banana" , "orange" , "grape" ])
4246 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 ))
4551 solution = word
4652 hint = f"Remove { num_chars_to_remove } letter(s) to reveal the original word."
4753 else : # puzzle_type == "logical"
@@ -54,6 +60,7 @@ def generate_medium_puzzle():
5460
5561 return puzzle , solution , hint
5662
63+
5764def generate_hard_puzzle ():
5865 puzzle_type = random .choice (["arithmetic" , "string" , "logical" ])
5966 if puzzle_type == "arithmetic" :
@@ -66,8 +73,10 @@ def generate_hard_puzzle():
6673 elif puzzle_type == "string" :
6774 word = random .choice (["python" , "programming" , "challenge" ])
6875 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 ))
7180 puzzle = "" .join (c if i not in indices_to_replace else replacement_chars [idx ]
7281 for idx , c in enumerate (word ))
7382 solution = word
@@ -82,36 +91,41 @@ def generate_hard_puzzle():
8291
8392 return puzzle , solution , hint
8493
94+
8595def main ():
8696 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+
89100 try :
90101 puzzle , solution , hint = generate_puzzle (difficulty_level )
91102 print ("\n Solve the puzzle:" )
92103 print (f"Question: { puzzle } " )
93-
104+
94105 attempts = 3
95106 while attempts > 0 :
96107 user_answer = input ("Your answer: " ).strip ()
97108 if user_answer .lower () == "hint" :
98109 print (f"HINT: { hint } " )
99110 else :
100111 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 )
102114 if user_answer == solution :
103115 print ("Congratulations! Your answer is correct." )
104116 break
105117 else :
106118 attempts -= 1
107119 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." )
109122 else :
110123 print (f"Sorry, the correct answer is: { solution } " )
111124 except ValueError :
112125 print ("Invalid input. Please enter a numeric answer." )
113126 except ValueError as e :
114127 print (e )
115128
129+
116130if __name__ == "__main__" :
117131 main ()
0 commit comments