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
>>>
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
>>>
1.0
2.0
3.0
Now quitting
>>>
(b)
Enter an integer : 2
1.0
2.0
3.0
>>>
1.0
2.0
3.0
>>>
n = int (input ("Enter an integer : "))
ReplyDeleteif n > 0 :
for aotu in range(1, n + n) :
print (a / (n / 2))
else :
print ("Now quitting")
??
DeleteWelcome : )
ReplyDeleteBoth the code frag are same so how could there be a difference between them.
ReplyDeleteNo the else statement is indented in the first code.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )