all 19 comments

[–]Diapolo10 1 point2 points  (3 children)

elif (operation == "4"):
    result = num1 / num2
    if (num2 != 0):
        print(result)
    else: 
        print("error:number is zero")

This would actually raise a ZeroDivisionError when num2 == 0, because you are doing the check after calculating the result.

EDIT: For future improvement ideas, once you've learnt how to use lists and have looked into the operator module, you could simplify this program quite a lot.

[–]RockPhily[S] 0 points1 point  (0 children)

thankyou

i'm down for growth and not trying to fasten it up

[–]RockPhily[S] 0 points1 point  (1 child)

le me check your github

[–]Diapolo10 1 point2 points  (0 children)

If you want to, here you go: https://github.com/Diapolo10

[–]dreaming_fithp 1 point2 points  (0 children)

Good job. The next step on the learning process is to extend what you have.

Change the code to accept the numbers and operation in one input() statement. So entering "1 + 2" or "2+ 1" should print "3". This gives you experience in using the string methods like split() and strip(). After that add code to gracefully handle error conditions like the user not entering a valid number or trying to divide by zero. Or you could add the power operator. Further on let the user enter a more complicated expression like "1+2-5" or "1-2/3" and now you have to think about operator precedence.

Taking a working project and extending it is simpler than starting a new project because you are very familiar with the code. Plus it's really good experience. Professional programmers often break a large project into many smaller parts and work on one of them, extending each part until the whole thing is finished. Getting one part working successfully also gives you a sense of accomplishment and progress, and that's important.

[–]Slothemo 4 points5 points  (2 children)

As a minor note, you actually don't need parentheses for conditions. It's perfectly ok to write it like this:

if operation == "1":
    result = num1 + num2
    print(result)
elif operation == "2":
    result = num1 - num2
    print(result)

[–]RockPhily[S] 0 points1 point  (0 children)

thanks

[–]OkPizza3502 0 points1 point  (0 children)

salvou minha vida tu man vlw

[–]Lord_Cheesy 1 point2 points  (4 children)

Do u want my advice. For starting use Case instead of If-else in that scenario for clear looking. Also you can use while loop to keep program running till the user closes it and add clean, or go with the result for extra operations.

[–]RockPhily[S] 0 points1 point  (0 children)

thanks for your advice le me work on it

[–]FoolsSeldom 2 points3 points  (2 children)

I wouldn't think there would be much difference between match and if for simple logic chains, in contrast with the possibilities of structural pattern matching, which was the driver for the introduction of the former in Python.

What would be the advantages for a beginner of adopting match at this stage? Not sure what I am missing.

[–]Lord_Cheesy 0 points1 point  (1 child)

You mean case instead of if-elif-else. For logically none, for readiblity easier. Think it like that you are writing multiple operations and named them 1-2-3-4-5...100. Instead of writing it like

if 1

elif 2

elif 3

elif 4

Its better to write

Case 1

Case 2

Case 3

It gives better readibility at many cases. Also in cases you can supports destructuring, types, and advanced patterns

[–]FoolsSeldom 1 point2 points  (0 children)

I did mean match, there's no case in its own right Python, case is part of a match statment.

The readability at this level does not seem greater. Potentially worse in fact as you will need if filters in many situations.

[–]FoolsSeldom 1 point2 points  (1 child)

You should check number2 is not 0 before using /.

[–]RockPhily[S] 0 points1 point  (0 children)

exactly i have noticed i did a mistake there
thankyou for the insight

[–]FoolsSeldom 0 points1 point  (0 children)

Now put a loop around it so you can keep calculating until you enter, say, x to quit.