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:
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:
- print str1*n1
- print str1 + str1[n2]
- print str1 + str2
- 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:
- print(str1*n1)
- print(str1*n2)
- print(str2*n1)
- print(str2 * n2)
- print(str1+ str2)