lundi 27 août 2018

How to write python code independent of code's position?

I am new in python and using Spyder for scripting. I have some python codes for nested if-else (multiple statements). But the problem is same code may give different outputs if someone shifted the codes. Following is an example of that kind of situation.

num1 = -1
num2 = -2

if(num1 > 0):
    if(num2 < 0):
        print("num1 +ve")
        print("num2 -ve")
elif(num2 < 0):
    print("num1 -ve")
    print("num1 -ve")
else:
    print("num1 -ve")
    print("num2 +ve")

# num1 -ve
# num1 -ve


if(num1 > 0):
    if(num2 < 0):
        print("num1 +ve")
        print("num2 -ve")
    elif(num2 < 0):
        print("num1 -ve")
        print("num1 -ve")
else:
    print("num1 -ve")
    print("num2 +ve")

# num1 -ve
# num2 +ve

Can I use any parenthesis or something like that so that code shift do not effect the output?

Aucun commentaire:

Enregistrer un commentaire