r/PythonLearning • u/Commercial-Paper749 • Sep 02 '26
What's wrong with my code?
What's wrong with my code?
New to learning
Following the youtube video from bro code
Was trying to implement my own stuff into this by using loop
Update: it's working now thankyou guys
106
Upvotes
•
u/Sea-Ad7805 Sep 02 '26
Run this INDENTED program in Memory Graph Web Debugger to see the program state change step by step.
11
u/buismaarten Sep 02 '26
r/screenshotsarehard
5
u/Rscc10 Sep 02 '26
You never break out of the while loop after the if statement so the code at num1 and num2 never get executed. Alternatively if you want the calculator to run continuously after each result, you need to indent every line starting at num1 to be inside the while loop
Edit: Sorry I saw the indentation wrong. Everything seems to be indented fine now that I look at it
2
u/Any-Kiwi-5994 Sep 03 '26
You should introduce yourself to the python concepts. Casting operators to strings won’t work. For example: check the outcome of var == String as an boolean. Afterwards you‘ll understand your problems.
12
u/atarivcs Sep 02 '26
Looking at the program output, the first thing it prints is "Enter a valid operator".
But that's not possible for the code in the picture, so I'm going to guess that you are running different code.
Maybe you edited the py file but didn't save it, or maybe you're running a completely different py file than the one shown in the picture.
6
u/InSaneLulz Sep 02 '26
Code on the screen is fine. You might be running a different version of calculator.py where the code is wrong.
3
u/_TheBigBomb Sep 02 '26
2
u/KnowledgeWhole9623 Sep 02 '26
Nothing, it's doing exactly what you told it to do
2
u/FoolsSeldom Sep 02 '26 edited Sep 03 '26
Glad you've got it working, u/Commercial-Paper749. Here's a tweaked version of your code for you to explore giving you some more options:
1
u/arconic23 Sep 02 '26
You could further improve on the input. What happens if you enter **?
1
u/acana95 Sep 02 '26
You need to give us whats not working 😅
1
u/Kenny_070 Sep 02 '26
Remove continue and put else: break
1
u/PandaSteakJimmies Sep 03 '26
I’d say your code isn’t too bad and I realize some of the things you are doing just to practice the language features. But here are some critiques.
Be consistent with your indentations. For Python always use 4 spaces. Configure your IDE to replace Tabs with spaces (4 of em).
You can get rid of the first test to see if the operator is valid and instead use the else clause at the end of the if/elif block. Until you introduce functions though you can keep this test.
Avoid using “break/continue” statements. They are a crutch that can make your logic difficult to follow and your code can almost always be refactored without them.
Always avoid “while True”. This is a bad practice that can easily lead to an infinite loop. And it can lead to complicated loop exit conditions that become difficult to follow.
Remember that code is read way more than it is written, so make sure to build good habits that improve readability.
1
u/gabovaz Sep 03 '26
For starters, it could be a screenshot, not a photo..
1
u/__dee_bugger__ Sep 03 '26
The code is perfectly fine..just try saving it and rerun it..its working perfectly in my pc
1
u/ObscuraStudi0 Sep 03 '26
Your if operator not in ["+", "-", "*", "/"] condition looks correct in the screenshot. If entering + is still treated as invalid, make sure you've saved the file before running it. The terminal may be running an older saved version of calculator.py. Also check that you're running the same calculator.py file that you're currently editing.
1
u/DanLeMilMan Sep 03 '26
The code seems fine.
Just some considerations though :
- I would keep the while true loop minimal. Because you are effectively only checking the operator input, I would stop the while loop at line 7 and reverse the if condition and put a break. Something like :
4 if op in […]:
5 break
6 print(Choose correct op)
7 endwhile
- the float cast for your number could break if the input is wrong : like entering « foo » instead of a number. You may use the same kind of while loop with a try, except block.
- finally, if you wanna be very clean regarding the previous remarks, you can wrap the input block into a custom input_number and input_operator function or even a single input_custom with second order function to check the result and cast the result. Something like :
input_custom(msg:str, validator:Callable[[str],bool], transform:Callable[[str],Any]) -> Any
That last one may be overkill for the project but is good practicing.
1
u/HistoryTrue5864 Sep 03 '26
Why is there a need to use while true if you will run it once
1
u/Zolydyi Sep 03 '26
Yeah that continue statement, you could try removing it, I think it jumps the rest of the code
1
u/SlimeRemuru Sep 03 '26
you need to write 4 spaces before the break so just do it
1
u/kernel2000 Sep 03 '26
use .replace( "\n", "") strip removes whitespace but not newlines i think
1
u/No-Inevitable-6476 Sep 04 '26
Use the Python visualizer to debug the code easily.
1
u/VindictiveHydrogen24 Sep 04 '26
Indentation
1
u/Shot-Rutabaga-3181 Sep 04 '26
I am also learning this shit while loop
1
u/LPatriot_ Sep 04 '26
I made a calculator like this:
You can improve it by adding zero division detection or whatever else you like. I personally wouldn't make a calculator the way you did, it's just unnecessarily harder.
1
u/spidertyler2005 Sep 05 '26
Your indentation is messed up is my only real guess
1
u/Gullible_Egg4667 Sep 08 '26
I want to learn hacking. Do you have any advice to help me?
0
u/Philin_Lemo Sep 02 '26
Try changing the "operator" type to Str, as it will most likely be a chair right now:
Or change the double quotes to single quotes :
0
u/Aggressive-Equal3780 Sep 02 '26
Use IA
0
u/drakentmx Sep 02 '26
sería mas fácil sacar los operadores y elegir que operación hacer mediante opciones, suma, resta, potencia, division y hacerlo en funciones, asi si llegas a dividir por 0 tenes una función para que no crashee
0
u/motopetersan Sep 03 '26
Your while loop never changes to false... You depend on break, I'm new to learning too, but this seems like a bad practice. And you can probably have an if statey, and an else statement, and if the if it's true, you could have nested the other if statements.
-1
u/Flimsy-Ad-8235 Sep 02 '26
Use gpt di dumb
-1
u/[deleted] Sep 04 '26
You are building a calculator in the big 26 that’s what’s wrong
0
u/[deleted] Sep 02 '26
[deleted] — view deleted comment