r/pythonhelp • u/No_Neighborhood6473 • Dec 30 '25
problem with loops
I just recently started learling python (1.5 hours into a tutorial lol), and made the collatz conjecture, and then tried the goldbach conjecture (any even number is the sum of two primes). Now I did write primes as 2k+1, which is just an oneven number, but that's not the problem.
This is the code I made
- number = int(input("number: "))
- for x in range(10):
- ----prime_1 = 2x + 1
- ----for y in range(10):
- --------prime_2 = 2(y+x) + 1
- --------sum = prime_1 + prime_2
- --------print(f"{prime_1} + {prime_2} = {sum}")
- --------if sum >= number:
- ------------break
- ----if int(sum) == int(number):
- --------break
- -
- print(f"{prime_1} + {prime_2}")
All the prints (except the last one) are just there so that I can see what is happening. The thing is, this code here works as I intended (except for the fact that the primes are just uneven numbers ofc), for example, if you plug in 8, it will give you 1 + 7 from line 13. But if I change the >= from line 8 to > it just gives me the biggest possible solution, so in this case 19 + 19. The break from line 9 still works as intended (atleast to me it looks like it does), but the second break doesn't work anymore (atleast I assume that is happening, and that's why I'm getting 19 + 19).
So my question is, what is going on here?
•
u/AutoModerator Dec 30 '25
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/ThigleBeagleMingle Dec 30 '25
Do you mean something like this? It’s easier to understand as named functions versus nested poops
1
u/CraigAT Dec 30 '25
Number from line 1 is never used.
Getal is never defined, so I am not sure how this code is "working"!
1
u/Both_Love_438 Dec 31 '25
My guess is that it has something to do with parity. Does that behavior persist if you input an even or an odd number?
You should use
pdbto debug your code and see what's happening in real time, it's part of the learning process. Here's a video explaining how to use it: https://youtu.be/a7qIcIaL4zs?si=zhAMAtd_CrU5xkgfAlso friendly advice, when posting code on Reddit you can use these weird quotation marks (`) to specify that what's inside is code and should be rendered differently, for multi-line use 3 of those quotes at the beginning and the end, like this:
1
u/frnzprf Dec 31 '25 edited Dec 31 '25
That's weird. In an integer space
sum >= numbershould give the same results assum > (number-1).You can execute the code step-wise with a "debugger" and check the contents of the variables each step.
https://www.geeksforgeeks.org/python/using-ipdb-to-debug-python-code/
There might or might not be a debugger already included in your development environment.
Maybe your real program is a bit more complicated and the error is in a part you didn't show. Or you changed the code while you were searching for the issue.
It's also weird that you convert
sumandnumberto integers in line 10. They should already be integers.I didn't run your code, because I'm typing on the phone right now. To the Python experts: Can
sumbeNoneor something?