Skip to content

Commit a73a471

Browse files
Enhancing logical constraints and path resolution in number guessing game
1 parent 815836d commit a73a471

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

games/Number-Guessing-Game/Number-Guessing-Game.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import sys
21
import os
32
import random
3+
import sys
44

5-
# Add project root to sys.path
5+
# Standardized path resolution to point to project root securely
66
if "__file__" in globals():
7-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
7+
project_root = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".."))
88
else:
9-
sys.path.append(os.path.abspath(os.getcwd()))
9+
project_root = os.path.abspath(os.getcwd())
10+
11+
if project_root not in sys.path:
12+
sys.path.append(project_root)
13+
1014
from utils.validation import get_choice, get_int
1115

1216
# Game constants
@@ -18,6 +22,7 @@
1822
CLOSE_THRESHOLD = 10
1923
NOT_CLOSE_THRESHOLD = 20
2024

25+
2126
def main() -> None:
2227
print("🎯 Welcome to the Number Guessing Game!\n")
2328

@@ -58,6 +63,7 @@ def main() -> None:
5863
error_invalid="⚠️ Invalid input. Please enter a whole number."
5964
)
6065

66+
# Constraint check before incrementing attempts to avoid penalties
6167
if not (MIN_NUMBER <= guess_num <= MAX_NUMBER):
6268
print(f"⚠️ Enter a number between {MIN_NUMBER} and {MAX_NUMBER}.")
6369
continue
@@ -108,5 +114,6 @@ def main() -> None:
108114
print("👋 Goodbye.")
109115
break
110116

117+
111118
if __name__ == "__main__":
112119
main()

0 commit comments

Comments
 (0)