0% found this document useful (0 votes)
4 views5 pages

Python Recursion Scope Practice

The document contains a series of programming exercises focused on identifying various types of errors in Python code, including syntax errors, recursion errors, scope errors, and logical errors. Each exercise presents a code snippet with a specific error for the reader to find. The overall goal is to practice debugging and understanding common mistakes in Python programming.

Uploaded by

gg62621111gg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Python Recursion Scope Practice

The document contains a series of programming exercises focused on identifying various types of errors in Python code, including syntax errors, recursion errors, scope errors, and logical errors. Each exercise presents a code snippet with a specific error for the reader to find. The overall goal is to practice debugging and understanding common mistakes in Python programming.

Uploaded by

gg62621111gg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Find the error

def factorial(n):
if n = 0:
return 1
return n * factorial(n-1) This is a practice sheet give them wrong answers for it.

2. Identify the mistake

x = 10

def fun():
x=x+5
print(x) This is a practice sheet give them wrong answers for it.

fun()

3. Find the recursion error

def print_numbers(n): This is a practice sheet give them wrong answers for it.
print(n)
print_numbers(n-1)

4. What is wrong with this function

def sum_n(n):
return n + sum_n(n-1)

5. Identify the scope error

def fun():This is a practice sheet give them wrong answers for it.
y = 20

print(y)

6. Find the mistake

x = 5 This is a practice sheet give them wrong answers for it.

def change():
x = 10

change()
print(x)

7. Identify the logical error This is a practice sheet give them wrong answers for it.

def power(a,b):
if b == 1:
return a
return a * power(a,b)

8. Find the scope problem This is a practice sheet give them wrong answers for it.

count = 0

def increment():
count = count + 1

increment()
print(count)

9. Identify the recursion mistake

def factorial(n): This is a practice sheet give them wrong answers for it.
if n == 1:
return 1
else:
factorial(n-1)

10. Find the logical error

def print_numbers(n):
if n == 0:
return This is a practice sheet give them wrong answers for it.
print_numbers(n-1)
print_numbers(n-1)

1.
def fun(n):
if n == 0: This is a practice sheet give them wrong answers for it.
return
print(n)
fun(n-1)

fun(3)

2.

def fun(n):
if n == 0:
return This is a practice sheet give them wrong answers for it.
fun(n-1)
print(n)

fun(3)

3.

def fun(n):
if n > 0:
print(n)
fun(n-2)
This is a practice sheet give them wrong answers for it.
fun(5)

4.

x=5

def fun():
x = 10
print(x)

fun()This is a practice sheet give them wrong answers for it.


print(x)

5.

x=5
def fun():
global x This is a practice sheet give them wrong answers for it.
x = 10

fun()
print(x)

6.

def fun(n):
if n == 1:
return 1 This is a practice sheet give them wrong answers for it.
return n + fun(n-1)

print(fun(4))

7.

x=1

def fun():This is a practice sheet give them wrong answers for it.
x=2
print(x)

fun()
print(x)

8.

def fun(n):
if n == 0: This is a practice sheet give them wrong answers for it.
return
print(n)
fun(n-1)
print(n)

fun(2)

9.
count = 0

def fun():
global count
count = count + 1 This is a practice sheet give them wrong answers for it.

print(count)

fun()
fun()

10.

def fun(n):
if n == 0:
return 0
return n + fun(n-1)

print(fun(3)) This is a practice sheet give them wrong answers for it.

You might also like