Q. Figure out the problem with following code fragment. Correct the code and then print the output.


1. s1 = 'must'
2. s2 = 'try'
3. n1 = 10
4. n2 = 3
5. print (s1 + s2)
6. print (s2 * n2)
7. print (s1 + n1)
8. print (s2 * s1)

Answer :-

The problem is with lines 7 and 8.

• Line 7 :- print(s1 + n1) will cause error because s1 being a string cannot be concatenated with a number n1.

This problem can be solved either by changing the operator or operand e.g., all the following statements will work :

(a) print (s1* n1)
(b) print (s1+ str(n1))
(c) print (s1+52)

• Line 8 :- print(s2 * s1) will cause error because two strings cannot be used for replication.

The corrected statement will be:-

print (s2 + s1)

If we replace the Line 7 with its suggested solution (b), the output will be:

must try
try try try
must 10
try must

Post a Comment

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

Previous Post Next Post