Professional Documents
Culture Documents
Functions Revision Worksheet_AK (1)
Functions Revision Worksheet_AK (1)
CHAPTER : FUNCTIONS
WELCOME OUTPUT
None 16
welcome 5
4
1
3. def f3(a,b): 4. def check(a):
global x,y for i in range(len(a)):
x=a+b a[i]=a[i]+5
a,y=a+x,a*x return a
print(a,b,x,y) b=[1,2,3,4]
f3(5,10) c=check(b)
print(f3(b=x,a=y)) print(c)
OUTPUT OUTPUT
20 10 15 75 [6, 7, 8, 9]
165 15 90 6750
None
2
11. def sum(*args): 12. def FunStr(S):
'''Function returns the sum of all Values''' T=""
r=0 for i in S:
for i in args: if i.isdigit():
r += i T=T+i
return r return T
print (sum.__doc__) X="PYTHON 3.9"
print (sum(1, 2, 3)) Y=FunStr(X)
print (sum(1, 2, 3, 4, 5)) print(X,Y,sep="*")
OUTPUT OUTPUT
Function returns the sum of all values PYTHON 3.9*39
6
15
Help on function sum in module __main__:
sum(*args)
Function returns the sum of all values
20#36#27#100#
3
III. Rewrite the following code in Python after removing all error(s) if any. Underline each
correction done in the code with appropriate explanation for the corrections done .
1.
CODE
#Error Q1
def findandprintsum():
# Identifiers cannot have special characters like &
count=sum=0
ans='y'
while ans=="y": #Comparison operator == must be used instead of assignment operator
num=int(input("enter a number :")) #identifier num not defined
if num < 0: #Statements should start on a newline
print("number entered is below zero. Aborting!")
break # : cannot be used along with break statement
sum=sum+num
count=count+1
ans=input("want to enter more numbers ?(y/n)..")
print("your entered", count,"numbers so far.")
print("sum of numbers entered is ",sum)
2. def increment(n):
n.extend(40)
return n[0],n[1],n[2],n[3]
L=[23,34,47]
m1,m2,m3,m4,m5=increment(L)
print(L(0:))
print(m1,m2,m3,m4)
print(L[3]=m4)
CODE
#Error Q2
def increment(n):
n.extend([40]) #Tn.extend(40) ypeError: 'int' object is not iterable
return n[0],n[1],n[2],n[3]
4
L=[23,34,47]
m1,m2,m3,m4=increment(L) #m1,m2,m3,m4=increment(L) ValueError: not enough values to
unpack (expected 5, got 4)
print(L[0:]) # print(L(0:)) SyntaxError: invalid syntax
print(m1,m2,m3,m4)
print(L[3],m4) # print(L[3],m4) Syntax Error assignment expression cannot be given in print
statmentExpression
3.
CODE
#Error Q3
def checksum(): # Syntax Error ( ) were missing
x=int (input("Enter the number")) # Logical Error String value must be converted into int
if x%2 == 0: #Syntax Error : Criteria must have two == signs for comparison
for I in range(2*x):
print(I) # NameError: name 'i' is not defined
else: # loop word must be removed
print("#")
5
4.
CODE
#Error Q4
total=0
# arg2 should have a default value before arg1 and colon missing
def sum(arg1,agr2=50):
total=arg1+agr2
print("Total :",total)
return total #use lowercase r for return statement
#__main__
ret=sum(10,20) #value can't be assigned to function call
print("Total :",total)