0% found this document useful (0 votes)
67 views6 pages

Python Functions: Error Fixes & Exercises

This document discusses functions in Python. It provides exercises to create Python functions with different argument types, identify errors in code snippets, predict output, and write functions to accomplish tasks like finding the maximum of three numbers, calculating the greatest common divisor of two numbers, and printing information about a city and its country. The objective is to practice creating Python functions using various arguments and performing different operations.

Uploaded by

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

Python Functions: Error Fixes & Exercises

This document discusses functions in Python. It provides exercises to create Python functions with different argument types, identify errors in code snippets, predict output, and write functions to accomplish tasks like finding the maximum of three numbers, calculating the greatest common divisor of two numbers, and printing information about a city and its country. The objective is to practice creating Python functions using various arguments and performing different operations.

Uploaded by

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

Programming Fundamentals (SWE-102) SSUET/QR/11

LAB # 07

FUNCTIONS

OBJECTIVE
Create python function using different argument types.

EXERCISE

A. Point out the errors, if any, and paste the output also in the following Python
programs.
1. Code
define sub(x, y)
return x + y

Errors:
- it should be def instead of define.
- : is missing after def statement.
- return should be in the loop.

Code:
#lab07
def sub(x, y):
return x + y

Output:

2. Code
define describe_pet(pet_name, animal_type='dog'):
print("\nI have a " , animal_type ,".")
print("My " , animal_type + "'s name is " , pet_name +
".")

Errors:
Programming Fundamentals (SWE-102) SSUET/QR/11
4

-pet_name is not given.

Code:
#lab07
def describe_pet(pet_name="Holder", animal_type='dog'):
print("\nI have a " , animal_type ,".")
print("My " , animal_type + "'s name is " , pet_name + ".")
describe_pet()

Output:

3. Code
def type_of_int(i):
if i // 2 == 0:
return 'even'
else:
return 'odd'

Errors:
- % should be used instead of //

Code:
#lab07
def type_of_int(i):
if i % 2 == 0:
return 'even’
else:
return 'odd'
print(type_of_int(7))

Output:
Programming Fundamentals (SWE-102) SSUET/QR/11
4

B. What will be the output of the following programs:


1. Code
def test(a):
def add(b):
a =+ 1
return a+b
return add
func = test(4)
print(func(4))

Output

2. Code
def return_none():
   return
print(return_none())

Output

3. Code
def test_range(n):
if n in range(3,9):
print( n,"is in the range")
else :
print("The number is outside the given range.")
test_range(7)
test_range(10)
test_range(4)

Output
Programming Fundamentals (SWE-102) SSUET/QR/11
4

C. Write Python programs for the following:

1. Write a function called favorite_book() that accepts one parameter, title. The function
should print a message, such as One of my favorite books is Alice in Wonderland. Call the
function, making sure to include a book title as an argument in the function call.

Code:

def print_info(bookname):
print("One of my favourite books is" , bookname)
return

print_info(bookname='Alice in Wonderland.')

Output:

2. Write a function called max( ), that returns the maxium of three integer numbers.

Code:

#lab07
a=eval(input("Enter the first number = "))
b=eval(input("Enter the second number = "))
c=eval(input("Enter the third number = "))
def max(a,b,c):
if(a>b and a>c):
result = a
elif(b>a and b>c):
result = b
elif(c>a and c>b):
result = c
return result
print("The maximum number is =",max(a,b,c))

Output:
Programming Fundamentals (SWE-102) SSUET/QR/11
4

3. Write a Python program to find GCD of two numbers

Code:

#lab07
a=eval(input("Enter the first number = "))
b=eval(input("Enter the second number = "))
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
print("The gcd of",a,"and",b,"is = ",gcd(a,b))

Output:

4. Write a function called describe_city() that accepts the name of a city and its country. The
function should print a simple sentence, such as Reykjavik is in Iceland. Give the parameter
for the country a default value. Call your function for three different cities, at least one of
which is not in the default country.

Code:

def print_info(cityname,countryname):
"This prints a passed info into this function"
print(cityname ,"is in", countryname)
return

print_info(cityname='Islamabad',countryname='Karachi')
print_info(cityname='New Delhi',countryname='India')
print_info(cityname='Sydney',countryname='Australia')
Output:
Programming Fundamentals (SWE-102) SSUET/QR/11
4

You might also like