|
1 | 1 | --- |
2 | | -title: "BUG: Prime number checker has inverted logic in basics/03_control_flow/02_check_prime.py" |
3 | | -labels: ["bug", "good first issue"] |
4 | | -assignees: [] |
| 2 | +title: "BUG: Prime number checker returns incorrect results" |
| 3 | +labels: ["bug", "good first issue", "priority: high"] |
5 | 4 | --- |
6 | 5 |
|
7 | 6 | ## Bug Description |
8 | 7 |
|
9 | | -The prime number checking logic in `basics/03_control_flow/02_check_prime.py` is inverted, causing incorrect results. |
| 8 | +The prime number checker in `basics/03_control_flow/02_check_prime.py` has inverted logic that returns wrong results. |
10 | 9 |
|
11 | 10 | ## Problem |
12 | 11 |
|
13 | | -The final if-else block has the logic reversed: |
14 | | - |
| 12 | +In the final if-else block (lines 13-16): |
15 | 13 | ```python |
16 | 14 | if(prime): |
17 | | - prime = False # This should be True |
| 15 | + prime = False # Wrong! Should be True |
18 | 16 | else: |
19 | | - prime = True # This should be False |
| 17 | + prime = True # Wrong! Should be False |
20 | 18 | ``` |
21 | 19 |
|
22 | 20 | ## Expected Behavior |
23 | 21 |
|
24 | | -- Prime numbers (2, 3, 5, 7, 11, etc.) should return `True` |
25 | | -- Non-prime numbers should return `False` |
26 | | - |
27 | | -## Current Behavior |
| 22 | +| Input | Expected | Actual | |
| 23 | +|-------|----------|--------| |
| 24 | +| 7 | True | False | |
| 25 | +| 10 | False | True | |
| 26 | +| 2 | True | False | |
28 | 27 |
|
29 | | -- Prime numbers return `False` |
30 | | -- Non-prime numbers return `True` |
| 28 | +## Fix |
31 | 29 |
|
32 | | -## Files Affected |
| 30 | +Remove lines 13-16 entirely. The logic before this block already sets `prime` correctly. |
33 | 31 |
|
| 32 | +## Files |
34 | 33 | - `basics/03_control_flow/02_check_prime.py` |
35 | 34 |
|
36 | | -## Suggested Fix |
37 | | - |
38 | | -Swap the assignments in the final if-else block: |
39 | | - |
40 | | -```python |
41 | | -if(prime): |
42 | | - prime = True |
43 | | -else: |
44 | | - prime = False |
| 35 | +## Testing |
| 36 | +```bash |
| 37 | +cd basics/03_control_flow |
| 38 | +python 02_check_prime.py |
| 39 | +# Enter 7 → should show True |
| 40 | +# Enter 10 → should show False |
45 | 41 | ``` |
46 | | - |
47 | | -Or simply remove the final if-else block entirely since the logic is already correct before it. |
48 | | - |
49 | | -## How to Test |
50 | | - |
51 | | -1. Run the script with input `7` (prime number) - should output `True` |
52 | | -2. Run the script with input `10` (non-prime) - should output `False` |
0 commit comments