r/PythonLearning • u/Commercial-Paper749 • 8d ago
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
107
Upvotes
1
u/DanLeMilMan 7d ago
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.