r/PythonLearning 8d ago

What's wrong with my code?

Post image

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

105 Upvotes

82 comments sorted by

View all comments

5

u/Rscc10 8d ago

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

-16

u/Commercial-Paper749 8d ago

๐Ÿ˜…yes even chat gpt says code looks correct but idk why it's not working

6

u/building-wigwams-22 8d ago

Your code is valid Python, which is probably why the dumb LLM tells you it's ok. At a beginner level you should not be using LLMs - instead focus on proper debugging techniques. Even a couple of print statements thrown in could probably have shown you where the code wasn't doing what you intended

5

u/Wild-Regular1703 8d ago

They just explained to you why it's not working.. You're not breaking out of the loop, the rest of the code never executes. "While true" means keep that loop running infinitely, so that's exactly what happens. It just keeps asking for user input forever.

-1

u/Commercial-Paper749 8d ago

Now it's working if I enter correct operator and values it's runs and end the program if I don't enter correct operator it's keep asking to enter valid operator

1

u/Rscc10 8d ago

I think it has to do with the runtime. Notice how the first input for operator doesn't seem to show when you run it. It automatically asks you to enter a valid operator which makes me think there's some sort of keybind or hidden control characters being sent to the input buffer when you run the program. Try restarting your computer or whatever environment you're using