Q. Consider the following code:


Inp = input("Please enter a string : ")

while len(Inp) <= 4:

    if Inp[-1] == 'z':

        Inp = Inp[0: 3] + 'c'

    elif 'a' in Inp :

        Inp = Inp[0] + 'bb'

    elif not int(Inp[0]):

        Inp ='1' + Inp[1:] + 'z'

    else:

        Inp = Inp + '*'

print (Inp)


What will be the output produced if the input is

(i) 1bzz,

(ii)' la'

(iii) abc

(iv)' Oxy',

(v)' xyz'!


Answer :-


(i)

Please enter a string : 1bzz

1bzc*

>>>

Explaination :

The length of the input string is 4, which satisfies the condition of the while loop.

The first if condition is not satisfied because the last character of the string is not 'z'.

The second elif condition is also not satisfied because there is no 'a' in the string.

The third elif condition is also not satisfied because the first character, '1', is an integer.

Therefore, the else block is executed, and the string becomes "1bzz*".


(ii)

Please enter a string : 1a

1bb**

>>>

Explaination :

The length of the input string is 5, which does not satisfy the condition of the while loop. Hence, the code does not enter the while loop.

The first if condition is not satisfied because the last character of the string is not 'z'.

The second elif condition is satisfied because there is 'a' present in the string. Consequently, the string becomes "1bb**" + "bb" = "1bb**bb".

The third elif condition is not checked since the while loop condition is not met.

The else block is not executed because none of the previous conditions was met.



(iii) endless loop because 'd will always remain at index 0 and condition 3 will be repeated endlessly.


(iv)

Please enter a string : 0xy

1xyc*

>>>

Explaination :

The length of the input string is 5, which does not satisfy the condition of the while loop. Hence, the code does not enter the while loop.

The first if condition is not satisfied because the last character of the string is not 'z'.

The second elif condition is not satisfied because there is no 'a' present in the string.

The third elif condition is also not satisfied because the first character, '1', is an integer.

The else block is executed because none of the previous conditions were met. Thus, the string becomes "1xyc*" + "" = "1xyc".


(v) Raises an error as Inp[0] cannot be converted to int.

4 Comments

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

Post a Comment

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

Previous Post Next Post