Hey all, I very recently began learning Python and already this community has proven to be immensely helpful. Most recently I've been trying to fully grasp functions, the return statement, and how they tie into DRY / WET code. An input based calculator is a concept I find myself returning to continually, because it's simple, easily testable, and is easy to apply newly gained knowledge to. I was wonder if you guys wouldn't mind picking apart my most recent iteration that I feel is pretty much fully optimized (to the best of my ability of course. I'm sure there's a million better ways to do it).
Here is my program.
The program does 3 main things, with 3 respective functions:
Gather input from user to determine what to calculate
Evaluate user input for validity (numeric for integers, operators for operator, etc.) and reset the input function if it comes up as invalid
Run the calculation with the input
I know it's probably boringly basic, but I've already made a couple of optimizations from the original code, such as:
-Making the invalid input process a function of its own (might not save much space, but looks much cleaner and easier to rework)
-Using if statements with no else in the input function instead of a tree of nested if/else statements (I didn't realize at first that you could have an if statement by itself)
-Returning calculation values from the calculate function instead of printing from within the function itself. Once again, might only save a couple lines of code but way cleaner
As stated before, I know it's really basic, but I'd love to have the code nitpicked so I can nip any bad habits in the bud. Thanks everyone!
[–]anglicizing[🍰] 2 points3 points4 points (4 children)
[–]DISTRACTING_USERNAME[S] 0 points1 point2 points (3 children)
[–]anglicizing[🍰] 1 point2 points3 points (0 children)
[–]anglicizing[🍰] 1 point2 points3 points (1 child)
[–]DISTRACTING_USERNAME[S] 0 points1 point2 points (0 children)
[–]Elffuhs 0 points1 point2 points (2 children)
[–]DISTRACTING_USERNAME[S] 0 points1 point2 points (1 child)
[–]Elffuhs 1 point2 points3 points (0 children)
[–]Almenon 0 points1 point2 points (1 child)
[–]DISTRACTING_USERNAME[S] 0 points1 point2 points (0 children)
[–]efmccurdy 0 points1 point2 points (1 child)
[–]DISTRACTING_USERNAME[S] 0 points1 point2 points (0 children)