Skip to content

Commit d7a69e8

Browse files
Merge pull request steam-bell-92#723 from Ashvin-KS/fix/bare-except
Fix bare except: clauses in Python game scripts
2 parents 7700142 + 6fdf996 commit d7a69e8

3 files changed

Lines changed: 7 additions & 4 deletions

File tree

games/Dots-Boxes-AI/Dots-Boxes-AI.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,12 @@ def ai_move():
286286
print(YELLOW + "⚠️ Line already taken!" + RESET)
287287
continue
288288
vertical_lines[row][col] = True
289-
except:
289+
except IndexError:
290290
print(RED + "❌ Position out of range!" + RESET)
291291
continue
292+
except ValueError:
293+
print(RED + "❌ Invalid input!" + RESET)
294+
continue
292295

293296
symbol = 'P' if current_player == 1 else 'A'
294297
got_box = check_boxes(symbol)

games/Flappy-Game/Flappy-Game.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def reset_game():
5151
try:
5252
with open(highscore_path, "r") as file:
5353
high_score = int(file.read())
54-
except:
54+
except (FileNotFoundError, ValueError, OSError):
5555
high_score = 0
5656

5757
def inside(point):

games/Math-Quiz/Math-Quiz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ def play_sound(sound_type):
3535
winsound.Beep(300, 300)
3636
elif sound_type == 'game_over':
3737
winsound.Beep(200, 600)
38-
except:
38+
except (RuntimeError, OSError):
3939
pass
4040

4141
def load_scores():
4242
if os.path.exists("math_quiz_scores.json"):
4343
try:
4444
with open("math_quiz_scores.json", "r") as f:
4545
return json.load(f)
46-
except:
46+
except (FileNotFoundError, json.JSONDecodeError, OSError, UnicodeDecodeError):
4747
pass
4848
return {"highest_score": 0, "longest_streak": 0}
4949

0 commit comments

Comments
 (0)