Give all the possible corrections for the errors in the following code after identifying the errors.

1. str1=’sure’

2. str2=’try it’

3. n1=10

4. n2=3

5. print str1+str2

6. print str2*n2

7. print str1 + n1

8. print str2*str1

 

Answer:

Untitled 36.jpg

Untitled 33.jpg

The first error is in line 7 . For string concatenation, both the operands must be string and for addition both the operands must be numeric . In line 7, str1 is of type string and str2 is of type int. 

Corrections for the above error will be:

  1. print str1*n1 
  2. print str1 + str1[n2]
  3. print str1 + str2  
  4. print str1 +chr(n1) 

The second error is in line 8. For replication operation, the second operand must be numeric and for concatenation, the operator must be *.

Corrections for the above error will be:

  1. print(str1*n1)  
  2. print(str1*n2)  
  3. print(str2*n1)  
  4. print(str2 * n2)  
  5. print(str1+ str2)

 

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.