Q. How are following two code fragments different from one another? Also, predict the output of following code fragments:

(a)

n = int (input ("Enter an integer : "))

if n > 0 :
    for a in range(1, n + n) :
        print (a / (n / 2))
    else :
        print ("Now quitting")

(b)

n = int (input ("Enter an integer : "))

if n > 0 :
    for a in range(1, n + n) :
        print (a / (n / 2))
else :
    print ("Now quitting")
 

Answer :-

Differences in both parts are followings:-

1. If we input a Negative number then there is no output in part (a) but in part (b) there will an output..

Example:-

(a)

Enter an integer : -4
>>>


(b)

Enter an integer : -4
Now quitting

>>> 

2. If we input positive number then there is "Now quitting" at the last line of the part (a) but no for (b).

(a)

Enter an integer : 2
1.0
2.0
3.0
Now quitting

>>>

(b)

Enter an integer : 2
1.0
2.0
3.0

>>> 


5 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. n = int (input ("Enter an integer : "))

    if n > 0 :
    for aotu in range(1, n + n) :
    print (a / (n / 2))
    else :
    print ("Now quitting")

    ReplyDelete
  2. Both the code frag are same so how could there be a difference between them.

    ReplyDelete
    Replies
    1. No the else statement is indented in the first code.

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post