[Type B] Working with Functions – Chapter 3 Sumita Arora
Working with Functions in Python Class 12 is chapter 3 from Sumita Arora book. There are function in python practice questions below, specifically type B or application based questions. We’ve also covered:
- flow of execution in python
- error finding questions in python
- output based questions in python
- function in python types (type A)
- user-defined functions in python (type A)
- built-in functions in python (type A)
- function in python example (type A)
- function in python with return type
- function in python with arguments
These all are topics from function in python class 12 chapter. You’ll also learn types of functions in python (type A) and practice some output based questions for class 12 level with answers. Keep scrolling to learn more! Type B questions are more containing function error finding questions in python class 12.
Table of Contents
Type B: Application Based Questions
1. What are the errors in following codes? Correct the code and predict output:
Solution: Let’s correct the code and identify the errors.
total = 0;
def sum( argi, arg2):
total = arg1 + arg2;
print("Total:", total)
return total;
sum( 10, 20);
print("Total:", total)
# Error 1: Inconsistent indentation
total = 0 # Removed semicolon unnecessary in Python
def sum(arg1, arg2): # Corrected parameter name
total = arg1 + arg2 # Corrected parameter name and indentation
print("Total:", total)
return total # Indentation corrected
result = sum(10, 20) # Assigned the return value to a variable
print("Total:", result) # Accessing the correct total variable
- (a) In short, Error:
total
inside the functionsum
is a local variable and doesn’t affect the globaltotal
.
In detail, there were issues with indentation, semicolon usage, incorrect parameter names, and incorrect return variable usage. These were corrected accordingly.
Changes:
- The error was in using
argi
instead ofarg1
in the function parameters. It’s crucial to use the consistent names to avoid confusion and errors. - The keyword for returning a value in Python is
return
, notRETURN
. Python is case-sensitive, so using the correct casing is essential.
Total: 30
Total: 0
def Tot(Number) #Method to find Total
Sum = 0
for C in Range (1, Number + 1):
Sum += C
RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])
# Error 1: Missing colon at the end of function definition
# Error 2: Incorrect capitalization of 'range'
# Error 3: Incorrect keyword 'RETURN' instead of 'return'
# Error 4: Incorrect syntax for function calls
def Tot(Number): # Corrected function definition
Sum = 0
for C in range(1, Number + 1): # Corrected capitalization and syntax
Sum += C
return Sum # Corrected return keyword
print(Tot(3)) # Corrected syntax for function calls
print(Tot(6)) # Corrected syntax for function calls
- (b) In short, Error:
Range
should berange
, andRETURN
should bereturn
.
In detail, errors included missing colons, incorrect capitalization of ‘range’, incorrect usage of ‘RETURN’ instead of ‘return’, and incorrect syntax for function calls. These were fixed.
Changes:
- The keyword for returning a value in Python is
return
, notRETURN
. - The correct function name to generate a range of numbers in Python is
range
, notRange
. Python is case-sensitive, so using the correct casing is essential.
6
21