+ 11
Consider the following code (disregarding potential exceptions raised if user inputs blabber: a = int(input("Enter your number: ") if a > 0: if a % 2 == 0: print(a, "is a positive, even number.") else: print(a, "is a positive, odd number.") elif a<0: if a % 2 == 0: print(a, "is a negative, even number.") else: print(a, "is a negative, odd number.") else: print(a, "equals zero.") As you can see, there are several nested "if" statements. The inner ifs are executed only if the outer if (in each case) is True. Nesting makes the code more clearly understood. Otherwise, you would have to test all combinations of all the conditions to test - in one line each time. That would be hard to maintain.
12th May 2017, 12:44 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar