Wednesday, December 24, 2014

Conditional statements

We will write some common programs to get you with the conditional statements


num=input()
num = int(num)
if num>3:
    print "number greater than 3"
else:
    print "number less than or equal to 3"

input:
4
Output:
number greater than 3

The above example seems simple but note the alignment after if and else statement that alignment should me maintained.

Now consider next example:

num=input()
num = int(num)
if num>3:
    print "number greater than 3"
    print "the number is: " + num
    if num>7:
        print "number is also greater than 7"
else:
    print "number less than or equal to 3"

input:
4
output:
number greater than 3
the number is: 4

input:
8
output:
number greater than 3
the number is: 8
number is also greater than 7

input:
2
output:
number less than or equal to 3

From the above example its quite clear from how alignment works for if else conditions.

we will see some more examples in next post.

No comments:

Post a Comment