Python Code Error Corrections Guide
Python Code Error Corrections Guide
His code
is having errors. Rewrite the correct code and underline the corrections made.
Def checkNumber(N):
status = N%2
return
#main-code
k=checkNumber(num)
if k = 0:
else:
Ans:
return return
#main-code #main-code
num=int( input(“ Enter a number to check :)) num=int( input(“ Enter a number to check :”))
k=checkNumber(num) k=checkNumber(num)
if k = 0: if k = = 0:
else: else:
Q2. Sameer has written a python function to compute the reverse of a number. He has
however committed a few errors in his code. Rewrite the code after removing errors also
underline the corrections made.
define reverse(num):
REV=0
While num > 0:
rem == num %10
rev = rev*10 + rem
Num=NUM//10
return rev
print(reverse(1234))
Ans:
Incorrect Code Correct Code
define reverse(num): def reverse(num):
REV=0 rev=rem=0
While num > 0: while num > 0:
rem == num %10 rem = num%10
rev = rev*10 + rem rev = rev*10 + rem
Num=NUM//10 num=num//10
return rev return rev
print(reverse(1234)) print(reverse(1234))
Q3. Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code.
Num=int(rawinput("Number:"))
sum=0
for i in range(10,Num,3)
sum+=1
if i%2=0:
print(i*2)
else:
print(i*3)
print (Sum)
Ans:
Incorrect Code Correct Code
Num=int(rawinput("Number:")) Num=int(input("Number:"))
sum=0 sum=0
for i in range(10,Num,3) for i in range(10,Num,3) :
sum+=1 sum+=1
if i%2=0: if i%2==0:
print(i*2) print(i*2)
else: else:
print(i*3) print(i*3)
print (Sum) print(sum)
Q4. Observe the following Python code very carefully and rewrite it after removing all
syntactical errors with each correction underlined.
DEF execmain():
x = input("Enter a number:")
if (abs(x)== x)
print("You entered a positive number")
else:
print("Number made positive:" x )
Ans:
Incorrect Code Correct Code
DEF execmain(): def execmain():
x = input("Enter a number:") x = int(input("Enter a number:"))
if (abs(x)== x) if (abs(x)== x):
print("You entered a positive print("You entered a positive
number") number")
else: else:
print("Number made positive:" x ) print("Number made positive:" ,x )
execmain()
Q5. Rewrite the following code in python after removing all syntax error(s). Underline each correction done in
the code.
30=Value
for VAL in range(0,Value)
If val%4==0:
print (VAL*4)
Elseif val%5==0:
print (VAL+3)
else
print(VAL+10)
Ans:
Incorrect Code Correct Code
30=Value Value=30
for VAL in range(0,Value) for val in range(0,Value):
If val%4==0: if val%4==0:
print (VAL*4) print (val*4)
Elseif val%5==0: elif val%5==0:
print (VAL+3) print (val+3)
else else:
print(VAL+10) print(val+10)
Q6. Ravi has written a function to print Fibonacci series for first 10 elements. His code is having
errors. Rewrite the correct code and underline the corrections made. some initial elements of
Fibonacci series are:
def fibonacci()
first=0,second=1
print(("first no. is ", first)
print("second no. is , second)
for a in range (1,9):
third=first+second
print(third)
first,second=second,third
fibonacci()
Incorrect Code Correct Code
def fibonacci() def fibonacci():
first=0,second=1
print(("first no. is ", first) first=0;second=1
print("second no. is , second)
for a in range (1,9): print("first no. is ", first)
third=first+second
print(third) print("second no. is ", second)
first,second=second,third
fibonacci() for a in range (1,9):
third=first+second
print(third)
first,second=second,third
fibonacci()
Q7. Aarti has written a code to input an integer and check whether it is even or odd. The code has
errors. Rewrite the code after removing all the syntactical errors, underlining each correction:
checkval def():
x = input("Enter a number")
if x % 2 == 0
print (x, "is even")
else;
print (x, "is odd")
Ans:
Incorrect Code Correct Code
checkval def(): def checkval():
x = input("Enter a number")
if x % 2 == 0 x = int(input("Enter a number"))
print (x, "is even")
else; if x % 2 == 0:
print (x, "is odd")
print (x, "is even")
else:
print (x, "is odd")
checkval()
Q8. Mithilesh has written a code to input a number and evaluate its factorial and then
finally print the result in the format : “The factorial of the <number> is <factorial value>”
His code is having errors. Rewrite the correct code and underline the corrections made.
f=0
num = input("Enter a number whose factorial you want to evaluate :")
n = num
while num > 1:
f = f * num
num -= 1
else:
print("The factorial of : ", n , "is" , f)
Incorrect Code Correct Code
f=0 f=1
num = input("Enter a number whose factorial you want to num = int(input("Enter a
evaluate :") number whose factorial you
n = num want to evaluate :"))
while num > 1: n = num
f = f * num while num > 1:
num -= 1 f = f * num
else: num -= 1
print("The factorial of : ", n , "is" , f) else:
print("The factorial of : ",
n , "is" , f)
Q9. Find error in the following code(if any) and correct code by rewriting code
and underline the
correction;‐
Def Errors()
print( x + y)
else:
print( x‐y)
Incorrect Code Correct Code
Def Errors() def Errors():
print(x-y)
Q10. Mohini has written a code to input a positive integer and display all its even factors in
descending order. Her code is having errors. Rewrite the correct code and underline the
corrections made.
n=input("Enter a positive integer: ")
for i in range(n):
if i%2:
if n%i==0:
print(i,end=' ')
Incorrect Code Correct Code
Def code() def code():
n=input("Enter a positive integer: ")
for i in range(n): n=int(input("Enter a positive integer: ") )
if i%2:
if n%i==0: for i in range(n):
print(i,end=' ')
if i%2:
define code()
if n%i==0:
print(i,end=' ')
code()
12
def foo(s1,s2): def sumList():
l1=[] data = [2,4,2,1,2,1,3,3,4,4]
l2=[] d = {}
for x in s1: for x in data:
[Link](x) if x in d:
for x in s2: d[x]=d[x]+1
[Link](x) else:
return l1,l2 d[x]=1
a,b=foo("FUN",'DAY') print(d)
print(a,b) sumList()
Output: sELCcME#DIi 5
def output(myvalue): def convert(line):
alpha = 0 n = len(line)
beta = "" new_line = ''
gama = 0 for i in range(0,n):
for i in range(1,6,2): if not line[i].isalpha():
alpha += i new_line = new_line + '@'
beta += myvalue[i-1]+ "#" else:
gama += myvalue[i] if line[i].isupper():
print(alpha, beta, gama) new_line = new_line + line[i]*2
else:
myvalue = ["A", 40, "B", 60, "C", 20] new_line = new_line + line[i]
output(myvalue) return new_line
130 # 100
def ChangeVal(M,N): def Call(P=40,Q=20):
for i in range(N): P=P+Q
if M[i]%5 == 0: Q=P-Q
M[i]//=5 print(P,'@',Q)
if M[i]%3 == 0: return P
M[i]//=3 R=200
L= [25,8,75,12] S=100
ChangeVal(L,4) R=Call(R,S)
for i in L: print(R,'@',S)
print(i,end="#") S=Call(S)
print(R,'@',S)
120 @ 100
300 @ 120
def Alpha(N1): T1 = tuple("Amsterdam")
while N1:
a=[Link]() def vowel(T1):
if a%5>2: T2, new_list = T1[1:-1], []
print(a,end='@') for i in T2:
else: if i in 'aeiou':
break j=[Link](i)
NUM=[13,24,12,53,34] new_list+=[j]
Alpha(NUM);print(NUM) print(new_list)
vowel(T1)
Output : [4, 7]
0OO
0OO3
0OO3*
0OO3*5
0OO3*5A
0OO3*5AN
0OO3*5ANP
0OO3*5ANPU
0OO3*5ANPUR
def Change(P ,Q=10): value = 50
P=P*Q def display(N):
Q=Q+P global value
print( P,"#",Q) value = 25
return (Q) if N%7==0:
A=5 value = value + N
B=10 else:
A=Change(A) value = value - N
B=Change(A,B)
print(A,"#",B) print(value, end="#")
display(20)
print(value)
Output: 50 # 60
Output: 50#5
600 # 610
60 # 610
a=20 x = 50
def call(): def func():
global a global x
b=20 print('x is', x)
a=a+b x = 20
return a print('Changed global+local x to', x)
print(a) func()
call()
print(a)
Output: 20
40 Output: x is 50
Changed global+local x to 20
def convert(Old): def Compy(N1,N2=10):
l=len(Old) return N1 > N2
New="" NUM= [10,23,14,54,32]
for i in range(0,l): for VAR in range (4,0,-1):
if Old[i].isupper(): A=NUM[VAR]
New=New+Old[i].lower() B=NUM[VAR-1]
elif Old[i].islower(): if VAR > len(NUM)//2:
New=New+Old[i].upper() print(Compy(A,B),'#', end=' ')
elif Old[i].isdigit(): else:
New=New+"*" print(Compy(B),'%',end=' ')
else:
New=New+"%"
return New
Older="InDIa@2022"
Newer=convert(Older)
print("New String is: ", Newer)
Output: 1 # 12
1#3
Output: ['E', 'a', 'i', 'a', 'i', 'n']
p=8 s="3 & Four"
def sum(q,r=5): n = len(s)
global p def Convert(s,n):
p=(r+q)**2 m=""
print(p, end= '#') for i in range(0, n):
a=2; b=5; sum(b,a) if (s[i] >= 'A' and s[i] <= 'Z'):
sum(r=3,q=2) m = m +s[i].upper()
elif (s[i] >= 'a' and s[i] <= 'z'):
Output : 49#25# m = m +s[i-1]
if (s[i].isdigit()):
m = m + s[i].lower()
else: m = m +'-'
return m
s=Convert(s,n)
print(s)
Output: 3---F-F-o-u-
def multiply(number1, number2) : def addEm(x,y,z):
answer = number1 * number2
return(answer) return(x+y+2)
print(number1, 'times', number2, '=',
answer) def prod(x,y,z):
output = multiply(5, 5)
print(output) return x*y*z
a= addEm(6,16,26)
Output : 25
b= prod(2,3,6)
print(a,b)
Output :24 36
Q12. Write a function modilst(L) that accepts a list of numbers as argument and
increases the value of the elements by 10 if the elements are divisible by 5. Also write a
proper call statement for the function.
For example:
Ans:
def modilst(L):
for i in range(len(L)):
if L[i] % 5 == 0:
L[i]+=10
L = [12,10,15,20,25]
modilst(L)
print(L)
Q13. Write a function lenFOURword(L), where L is the list of elements (list of words) passed as
argument to the function. The function returns another list named ‘indexList’ that stores the
indices of all four lettered word
of L.
For example:
Ans:
def lenFOURword(L):
indexList=[]
for i in range(len(L)):
if len(L[i])==4:
[Link](i)
return indexList
L=["DINESH", "RAMESH", "AMAN", "SURESH", "KARN"]
print(lenFOURword(L))
Q14. Write a python function displaywords() that will print all the words that are
having length greater than 3.
Example:
Ans:
strings='A man always wants to strive higher in his life. He wants to be perfect.'
def displayword(strings):
words=[Link]()
for i in words:
if len(i)>3:
print(i,end=' ')
displayword(strings)
Q15. Write a python function countvowel() that reads the contents of the string and
counts the occurrence of vowels(A,E,I,O,U) in the file.
Ans:
def countvowels(st):
c=0
for i in st:
if i in 'aeiouAEIOU':
c+=1
return c
st="The quick brown fox jumps over the lazy dog"
count=countvowels(st)
Q16. Ravi a python programmer is working on a project, for some requirement, he has to
define a function with
But this code is not working, Can you help Ravi to identify the error in the above function and
what is the solution.
Ans:
Q17. What do you understand the default argument in function? Which function parameter
must be given default argument if it is used? Give example of function header to illustrate
default argument.
Ans:
Default argument in function- value provided in the formal arguments in the definition header
of a function is called as default argument in function. They should always be from right side
argument to the left in sequence. For example:
Q18. Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the
function. The function returns another list named ‘indexList’ that stores the indices of all Non-
Zero Elements of L.
For example:
If L contains [12,4,0,11,0,56]
Ans:
def INDEX_LIST(L):
indexList = [ ]
for i in range(0,len(L)):
if (L[i]%2 == 0):
[Link](i)
return indexList
L =[12,4,15,11,9,56]
print(INDEX_LIST(L))
Output
Ans:
def LeftShift(Numlist,n):
NumList=Numlist[n:]+Numlist[:n]
return NumList
n=2
print("Original List",Numlist)
Q20. Write a function SQUARE_LIST(L), where L is the list of elements passed as argument to
the function. The function returns another list named ‘SList’ that stores the Squares of all Non-
Zero Elements of L.
For example:
If L contains [9,4,0,11,0,6,0]
Ans:
def SQUARE_LIST(L):
SList=[]
for i in L:
if i!= 0:
[Link](i*i)
return SList
L=[9,4,0,11,0,6,0]
print(SQUARE_LIST(L))
Q21. Write a function in Python Convert() to replaces elements having even values with
its half and elements having odd values with twice its value in a list.
Ans:
def Convert(L):
SList=[]
for i in L:
if i%2== 0:
[Link](i//2)
else:
[Link](i*2)
return SList
L=[3,4,5,16,9]
print(Convert(L))
Ans:
def AdjustList(lst):
print(list(reversed(lst)))
AdjustList(lst)
The function returns another list named „evenList‟ that stores the indices of all even
numbers of L.
For example:
If L contains [12,4,3,11,13,56]
Ans:
def EVEN_LIST(L):
evenList=[]
for i in L:
if i%2==0:
[Link](i)
return(evenList)
L=[12,4,3,11,13,56]
L2=EVEN_LIST(L)
For example :
If the Nums contains [25,24,35,20,32,41]
Ans:
def DoubletheOdd(Nums):
s=0
for i in Nums :
if i%2!=0:
s+=i*2
Nums=[25,24,35,20,32,41]
DoubletheOdd(Nums)
def SwapHalfList(Array):
s1=s2=0
L=len(Array)
for i in range(0,L//2):
s1+=Array[i]
s2+=Array[i]
if s1>s2:
for i in range(0,L//2):
Array[i],Array[i+L//2]=Array[i+L//2],Array[i]
SwapHalfList(L)
print(L)
Q26. Write a function INDEX_LIST(L), where L is the list of elements passed as argument
to the function. The function returns another list named „indexList‟ that stores the indices
of all Elements of L which has a even unit place digit.
For example:
If L contains [12,4,15,11,9,56]
Ans:
def Index_List(L):
index_List=[]
for i in range(len(L)):
if L[i]%2==0:
index_List.append(i)
return index_List
L=[12,4,15,11,9,56]
print(L)
idx=Index_List(L)
print(idx)
list of VALUES.
Ans:
def AddOdd(Values):
n=len(Values)
s=0
for i in Values:
if (i%2!=0):
s=s+i
print(s)
Values=[10,3,24,5,7,9,40]
AddOdd(Values)
Q28. Write the definition of a function Sum3(L) in Python, which accepts a list L of integers
and displays the sum of all such integers from the list L which end with the digit 3.
then the function should display the sum of 123, 13, 23, i.e. 159 as follows :
Ans:
def sum3(L):
s=0
for i in L:
if i%10==3:
s=s+i
return s
print(sum3(L))