In: Computer Science
For each of the following Python errors provide a scenario that would cause it:
Ans:
# it generates type error
# here we are concatinating string with integer
print("st"+5)
# it generates attribute error
# integer variable val has no attribute append
val=10
val.append(20)
# index error occurs if we try to access something that
is not exist
# here lst have length 3 so, indices ranges from 0 to 2 only. so
accessing index 4 generates error.
lst=[1,2,3]
print(lst[4])
# if we try to convert to some other datatype that can't
be convertable
# here "dog" cannot be converted to int
val=int("dog")
# ZeroDivisonError occurs if we divison with 0
res=5/0
# Indentation error occurs if we don't follow correct
indentation
# here after for loop , the content of for loop must be inside(a
tab space) of it.
lst=[1,2,3,4]
for val in lst:
print(val)