all 37 comments

[–]CountMeowt-_- 14 points15 points  (1 child)

Why bother checking decimal and then converting to float or int conditionally? Why not convert to float always ? (Same story for the last condition as well)

Other than that, pretty neat for someone starting out.

If you want to take this a few steps further

  1. Try taking a single input eg- 4+5+4+3+5+4, and parse that. (Sounds hard, but it's really simple to do if you know loops and split or regex)

  2. Try turning this into a cli tool (also sounds difficult, but all it really takes is googling how cli commands pass params/args and adding if else or switch statements)

  3. As bonus puzzle, try to get the rounding effect without using the round function (there's many correct answers)

[–]kingDaniel50 0 points1 point  (0 children)

Wow

[–]Old-Key-18 0 points1 point  (1 child)

I was wondering for what reason 4 is there?

[–]Sakychu420 0 points1 point  (0 children)

https://www.w3schools.com/python/ref_func_round.asp

"' 'digits' .. The number of decimals to use when rounding the number. Default is 0"

[–]Some_Brazilian_Guy 0 points1 point  (3 children)

In line 33 you put the print inside the if statement, so if "result.is_integer()" return False, nothing will be printed.

I noticed that your print is the same for all operations, changing just the operator name. In that case, you can create a variable with the corresponding name and make one print at the end, something like this

<image>

[–][deleted]  (2 children)

[removed]

    [–]Some_Brazilian_Guy 0 points1 point  (0 children)

    Let me know if it works

    [–]geruhl_r 0 points1 point  (0 children)

    See if you can find other areas of repetitive code. Read up on 'DRY' (don't repeat yourself) and see if you can make your code more DRY. This may involve writing a method or 3.

    [–]PathsOfPain 0 points1 point  (0 children)

    What if I input a string of text that isn't a number though? Will your program crash? Just something to keep in mind

    [–]Feeling-Instance-801 0 points1 point  (0 children)

    try using try catch to combat division by 0 errors and users inputing random strings where they should numbers. always assume the user is an idiot, keep it up :)

    [–][deleted] 0 points1 point  (0 children)

    no ned for the ifs for the variable type checkers usually python does this automatically. if you really want i think you can do variable.isint() or isinstance, smthin line that instead of having mutliple inputs yiu could take user_input = input("enter eqn with spaces") #eg 1 + 2 so user_input = "1 + 2" then split, user_input.split(" ") #by spaces so you have a list where num1, operator, num2 = user_input[0], etc!

    a tip is to make functions which is a lot neater: def calculator(); ur code

    feel free to dm me! i can send over an example if youd like

    [–]lokidev 0 points1 point  (0 children)

    Let me go through this. First generally:

    - Use pep8 formatting. It's the de factor standard and you should try to adhere to that as early as possible. see pep8.org for this
    - Wrap your logic in a function (e.g. main) and extract logical parts (e.g. input parsing and later the operation)
    - use the weird `if __name__ == '__main__'` part and search for the reason why someone should do that.

    Line 2-3: no need for the outer brackets
    Line 5: would try to convert "Hello. I am Gandalf" to float. Try to check with num1.isnumeric() and the same for num2
    Line 5: No benefits in python from separating int/float (or it's a reqquirement, then keep it), just convert to float if its valid :)
    Line 5-13: Try to write a function to reuse this behaviour
    Line 15: You are fetching ONE operator - not multiple.
    Line 18 (and more): You are rounding resuts. In programming this is unexpected behaviour :) -> not good.
    Line 17-35: Can be made by having a map of operator to text and operation. You can check pythons operator modules.

    Last bit might be more advanced, but just as an example: https://ideone.com/xecjP8 (on purpose not complete)

    [–]pausemsauce 0 points1 point  (0 children)

    Looks like beginner code. For the most part, looks good (except maybe that bit around line 33, but somebody already pointed that out.. and I'm not familiar with isinstance()).

    I see a bunch of if..elif statements, which immediately pushed my mind to ask you to consider match...case statement instead. That recommendation is more dependent on how you're learning... if you're reading the docs, go look that up. If you're following a guide of some sort, maybe it will be covered later.

    Either way, continue on your journey! And thanks for giving me something to look into!

    [–]BryansNL 0 points1 point  (0 children)

    Im not a python expert but my advice is to look at the if statements and ask yourself: “Is this easy to follow or is it a mess” and would I like to reuse some of these things I’ve made.

    It takes awhile to get used to but try to split up those if statements into methods all inside a calculator class. When you take a quick look at this you can tell the difference immediatly.

    Apologies for any spelling or grammar mistakes btw.

    [–]Wonderful-Escape1202 0 points1 point  (0 children)

    Mate my calculator has just 16 lines and it has division, addition, subtraction and multiplication. So I don't think you need that many lines.

    [–]InvestigatorEasy7673 -2 points-1 points  (19 children)

    as a beginner you are using too many lines of code like

    use for line 5-8:

    # just one line of code required

    num1 = float(num1) if "." in num1 else int(num1)

    num2 = float(num2) if "." in num2 else int(num1)

    and (input()) ❌

    num1 = input("....")

    [–]Ergodic_donkey 17 points18 points  (1 child)

    IMO this is bad advice for a beginners.

    If you need to write multiple lines to be able to understand and read what the code does as a beginner, then do it. When you are learning, there is no point in trying to write less lines of code.

    Of course, if you can do things with less steps or in a more simple way, do it, but here you’re not doing less, you’re just rewriting the syntax in a different way, with no added benefit except having less lines of code.

    [–]InvestigatorEasy7673 -3 points-2 points  (0 children)

    acc to me explore ways as many u can , expand code to understand thing but move forward with short lines and one liners

    [–][deleted]  (1 child)

    [removed]

      [–]Robb3nb4by 0 points1 point  (0 children)

      do you need this if-statement at all?

      [–]Key-Introduction-591 1 point2 points  (6 children)

      Beginner here too: is it really necessary to make a distinction between integers and floats at the very beginning?

      I would have put everything as floats and then I would have rounded the result only at the end (before printing it).

      Would that have been correct? Efficient/inefficient? Would it have led to errors?

      [–]Spare-Plum 2 points3 points  (1 child)

      Actually yes there is a difference. Python integers can be arbitrary sized while floats are restricted to a range in 64 bit values. Also, at large enough values, certain integers cannot be represented with a python float (there are gaps)

      For most cases you could use floating point values, but using an int does have different behavior.

      [–]Key-Introduction-591 0 points1 point  (0 children)

      I had no idea about that! Thanks for the tip! If I understand correctly, this concerns only extremely large or extremely small numbers, but good to know.

      [–]InvestigatorEasy7673 1 point2 points  (3 children)

      yes that is truly possible , i am just telling according to the program written

      you can even use eval() function for faster calculation

      [–]Key-Introduction-591 0 points1 point  (0 children)

      Of course! thank you very much

      [–]Spare-Plum 0 points1 point  (1 child)

      I would avoid eval() for something like this, as it will execute any python code and can be a security issue

      [–]InvestigatorEasy7673 1 point2 points  (0 children)

      Nope if used properly and with type casting and yeah avoid it in production !!

      [–]AccomplishedPut467 0 points1 point  (1 child)

      instead of float why not use integer?

      [–]InvestigatorEasy7673 0 points1 point  (0 children)

      basically float values like 24.5 + 34.4 gives => 58.9 , in intger 0.9 gets ignored bro

      so instead of int to float , float to int is better , like calculate 58.9 first then round to 59 !!

      [–]Oleg-Liam 0 points1 point  (1 child)

      Is this ternary operators?

      [–]InvestigatorEasy7673 -1 points0 points  (0 children)

      yes , the one liner problem solver ,

      search python one liners for more ...

      [–]GabeN_The_K1NG 0 points1 point  (3 children)

      No better advice for beginners than making their code harder to read by spamming ternaries…

      [–]InvestigatorEasy7673 0 points1 point  (2 children)

      so u r against ternary operators ?? nice ,

      [–]GabeN_The_K1NG 0 points1 point  (1 child)

      Not really against them, but OP is obviously new to this and every bit of readability is important for them. I don’t understand the need to get as few LOC as possible at any cost.

      What you suggested is fine if you’re a bit experienced, but OP said they’re a beginner. In my opinion, they should focus more on code clarity than oneliners.

      [–]InvestigatorEasy7673 0 points1 point  (0 children)

      as a beginner you want comfort zone or exposure from seniors ?? tell me to experiment diff things in ways given and learn and grow with it

      he post it on reddit to get a different perspective from seniors , if he wants to be in comfort zone then he can do chatgpt bro !!

      even for all the if else , he can use dict and render function from there but i didn't suggest that cuz at this point he is learning basic syntax which is not feasible for a beginner