0% found this document useful (0 votes)
3 views38 pages

Python Lesson 4b Notes - Updated 1

The document provides a comprehensive guide on defining and using functions in Python, including user-defined functions, arguments, return statements, and examples such as checking if a number is odd or even. It covers various topics like keyword arguments, default arguments, and includes classwork exercises for practical application. The document serves as a resource for understanding function modularity and reusability in programming.

Uploaded by

gigicho0815
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)
3 views38 pages

Python Lesson 4b Notes - Updated 1

The document provides a comprehensive guide on defining and using functions in Python, including user-defined functions, arguments, return statements, and examples such as checking if a number is odd or even. It covers various topics like keyword arguments, default arguments, and includes classwork exercises for practical application. The document serves as a resource for understanding function modularity and reusability in programming.

Uploaded by

gigicho0815
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
You are on page 1/ 38

P.

1 of 38

Contents

Functions in Python ....................................................................................................... 2


Define Function ...................................................................................................... 2
Function takes arguments ...................................................................................... 3
Elaboration of oddEven.py ............................................................................. 4
Elaboration of oddEvenWithReturn.py .......................................................... 7
Elaboration of oddEvenWithReturn - by variable.py ................................... 10
Classwork 1 .................................................................................................. 13
More about Arguments ............................................................................... 15
Keyword Arguments............................................................................. 15
Elaboration of keywordFunc.py ................................................... 17
Default Arguments ............................................................................... 19
Elaboration of defaultFunc.py ...................................................... 21
More about return statement...................................................................... 24
Return values in list type ...................................................................... 24
Elaboration of defaultFuncModified1.py ..................................... 26
Elaboration of defaultFuncModified2.py ..................................... 30
Looping and Function .......................................................................................... 33
Self-defined temperature conversion function............................................ 33
Elaboration of temperatureFunction.py .............................................. 34
Classwork 2 .......................................................................................................... 37
P. 2 of 38

Functions in Python
A function is a block of organized and reusable code that can perform a single,
related action. Functions provide better modularity for the program and a higher
degree of code reusing.

As you already know, Python gives you many built-in functions like print(), range()…
etc. We, instead, can also create our own functions. These functions are
called user-defined functions.

Define Function
Follow the guidelines below to set definitions of the functions.

➢ Function blocks begin with the keyword def followed by the function name
and parentheses ( ).

➢ Any input parameters or arguments should be placed within these


parentheses. You can also define parameters inside these parentheses.

➢ The code block within every function starts with a colon : and is indented.

The general syntax of a function will be:

def function-name(Parameter list):


statements, i.e. the function body
P. 3 of 38

Function takes arguments


The following function definition takes x as input parameter and analyzes its nature
of odd or even by the if structure. The function then prints out the result on screen
Now, create a new python script named oddEven.py and type in the code listing as
shown below:

# oddEven.py
# A simple Python function to check
# whether x is odd or even

# This is the definition of the user-defined function


def oddEven( x ):
if (x % 2 == 0):
print ("%d is even number!" %x)
else:
print ("%d is odd number!" %x)

# Call and invoke the function to work


print("The \"oddEven\" function is called for 2 times: \n")
oddEven(18)
oddEven(19)

This is the result you should obtain:


P. 4 of 38

Elaboration of oddEven.py
1. Function Definition
def oddEven(x):

➢ The oddEven function is defined to take one parameter, x.


➢ This function determines whether the given number x is odd or even and
prints the result.

2. Conditional Logic
if (x % 2 == 0):
print("%d is even number!" % x)
else:
print("%d is odd number!" % x)

➢ Purpose: The function uses an if-else statement to check the parity of the
number x.
➢ Explanation:
✓ x % 2 == 0:
 If the remainder when x is divided by 2 is 0, the number is
even.
✓ else:
 If the condition x % 2 == 0 is False, the number is odd.
➢ String Formatting:
✓ The %d placeholder is used to insert the value of x into the printed
string.
P. 5 of 38

3. Calling the Function


print("The \"oddEven\" function is called for 2 times: \n")
oddEven(18)
oddEven(19)

➢ First Call:
oddEven(18)

✓ The function is invoked with x = 18.


✓ The condition 18 % 2 == 0 is True because the remainder is 0.
✓ Output:
18 is even number!

➢ Second Call:
oddEven(19)

✓ The function is invoked with x = 19.


✓ The condition 19 % 2 == 0 is False because the remainder is 1.
✓ Output:
19 is odd number!

4. Print Statement Before Function Calls


print("The \"oddEven\" function is called for 2 times: \n")

➢ This line prints a message to indicate that the oddEven function will be
invoked twice.
➢ The use of escape characters (\") allows quotation marks to appear in the
output string.
P. 6 of 38

On the other hand, you may also write the function with return statement. Try the
following code listing named oddEvenWithReturn.py, it generates the same result as
the previous program oddEven.py.

# oddEvenWithReturn.py
# A simple Python function to check
# whether x is odd or even

# This is the definition of the user-defined function


def oddEven( x ):
if (x % 2 == 0):
return "even"
else:
return "odd"

# Call and invoke the function to work


print("The \"oddEven\" function is called for 2 times: \n")

result = oddEven(18)
print("18 is %s number!" %result)
result = oddEven(19)
print("19 is %s number!" %result)

You should get the same output as the program OddEven.py:


P. 7 of 38

Elaboration of oddEvenWithReturn.py
1. Function Definition
def oddEven(x):
if (x % 2 == 0):
return "even"
else:
return "odd"

➢ The function oddEven is defined to take one parameter, x.


➢ Purpose: The function determines whether the number x is odd or even and
returns the corresponding string "even" or "odd".
➢ Logic:
✓ x % 2 == 0:
The modulo operator (%) checks if the remainder when x is
divided by 2 is 0.
 If this condition is True, the number is even, and the function
returns "even".
✓ else:
If the condition x % 2 == 0 is False, the number is odd, and the
function returns "odd".
➢ Return Statement:
✓ The return keyword specifies the value the function outputs when it is
called.
✓ Returns "even" or "odd" depending on the parity of x.

2. Calling the Function


print("The \"oddEven\" function is called for 2 times: \n")

➢ This line prints a message indicating that the oddEven function will be
invoked twice.
➢ The use of escape characters (\") allows quotation marks to appear in the
output.
P. 8 of 38

3. First Function Call


result = oddEven(18)
print("18 is %s number!" % result)

➢ Function Call:
result = oddEven(18)

✓ The function oddEven is called with x = 18.


✓ Inside the function:
 18 % 2 == 0 evaluates to True because the remainder is 0.
 The function returns "even".
✓ The returned value "even" is stored in the variable result.

➢ Output Statement:
print("18 is %s number!" % result)

✓ %s is a placeholder for a string, which is replaced by the value of


result.

4. Second Function Call


result = oddEven(19)
print("19 is %s number!" % result)

➢ Function Call:
result = oddEven(19)

✓ The function oddEven is called with x = 19.


✓ Inside the function:
 19 % 2 == 0 evaluates to False because the remainder is 1.
 The function returns "odd".
✓ The returned value "odd" is stored in the variable result.

➢ Output Statement:
print("19 is %s number!" % result)

➢ %s is a placeholder for a string, which is replaced by the value of


result.
P. 9 of 38

For program oddEvenWithReturn.py can so be written as the code listed in


oddEvenWithReturn - by variable.py.

oddEvenWithReturn - by variable.py

# oddEvenWithReturn.py
# A simple Python function to check
# whether x is odd or even

# This is the definition of the user-defined function


def oddEven( x ):
if (x % 2 == 0):
return "even"
else:
return "odd"

# Call and invoke the function to work


print("The \"oddEven\" function is called for 2 times: \n")
#result = "even"
k=18
result = oddEven(k)
print("%d is %s number!" %(k,result))
#result = "odd"
result = oddEven(19)
print("19 is %s number!" %result)

ch = input('')
P. 10 of 38

Elaboration of oddEvenWithReturn - by variable.py


1. Function Definition
def oddEven(x):
if (x % 2 == 0):
return "even"
else:
return "odd"

➢ The function oddEven is defined to take one parameter, x.


➢ Purpose: The function determines whether the number x is odd or even and
returns the corresponding string "even" or "odd".
➢ Logic:
✓ x % 2 == 0:
The modulo operator (%) checks if the remainder when x is
divided by 2 is 0.
 If this condition is True, the number is even, and the function
returns "even".
✓ else:
If the condition x % 2 == 0 is False, the number is odd, and the
function returns "odd".
➢ Return Statement:
✓ The return keyword specifies the value the function outputs when it is
called.
✓ Returns "even" or "odd" depending on the parity of x.

2. Calling the Function


print("The \"oddEven\" function is called for 2 times: \n")

➢ This line prints a message indicating that the oddEven function will be
invoked twice.
➢ The use of escape characters (\") allows quotation marks to appear in the
output.
P. 11 of 38

3. First Function Call


k = 18
result = oddEven(k)
print("%d is %s number!" % (k, result))

➢ Variable Assignment:
k = 18

✓ The variable k is assigned the value 18.

➢ Function Call:
result = oddEven(k)

➢ The function oddEven is called with x = k, where k = 18.


➢ Inside the function:
 18 % 2 == 0 evaluates to True because the remainder is 0.
 The function returns "even".
➢ The returned value "even" is stored in the variable result.

➢ Output Statement:
print("%d is %s number!" % (k, result))

✓ %d is a placeholder for an integer (k), and %s is a placeholder for a


string (result).
✓ The values of k and result are substituted into the string.
P. 12 of 38

4. Second Function Call


result = oddEven(19)
print("19 is %s number!" % result)

➢ Function Call:
result = oddEven(19)

✓ The function oddEven is called with x = 19.


✓ Inside the function:
 19 % 2 == 0 evaluates to False because the remainder is 1.
 The function returns "odd".
✓ The returned value "odd" is stored in the variable result.

➢ Output Statement:
print("19 is %s number!" % result)

✓ %s is a placeholder for a string, which is replaced by the value of


result.
P. 13 of 38

Classwork 1
Try to write a Python Program (factorialFunc.py) that can calculate the factorial of an
integer. In Mathematics, the factorial of a non-negative integer n, denoted by n!, is
the product of all positive integers less than or equal to n.

For instance, 4! = 1*2*3*4

But notice that the factorial of 0 should be 1.

In factorialFunc.py, you are requested to add a self-defined function called factFunc( )


which takes 1 argument. The aim of this self-defined function is calculating the
factorial and return the calculated answer to the caller.

Please refer to the following figures for reference before writing the Python program,
factorialFunc.py:

Figure 1
P. 14 of 38

Figure 2
P. 15 of 38

More about Arguments

Keyword Arguments

When using keyword arguments in a function call, the caller identifies the arguments
by the parameter name.

This will allow the skipping of arguments or placing them out of order because the
Python interpreter is able to use the keywords provided to match the values with
parameters.

See the following script, keywordFunc.py, for your reference:

#keywordFunc.py

# Function definition is here


def printInfo( name, age ):
print ("My name is : %s" % name)
print ("And my age is : %d" % age)

#Python allows usage of return statement even nothing needs to be returned


return

# Now you can call printInfo function


printInfo( age=28, name="Kimkim" )

ch = input('')
P. 16 of 38

And here is the output after running the above script:


P. 17 of 38

Elaboration of keywordFunc.py
1. Function Definition
def printInfo(name, age):

➢ Function Name: printInfo


✓ This is the name of the function. It is used to reference and call the
function later.

➢ Parameters: name and age


✓ These are the inputs (parameters) to the function. When the function
is called, values will be passed to these parameters.
✓ name is expected to be a string, and age is expected to be an integer.

2. Printing Statements
print("My name is : %s" % name)
print("And my age is : %d" % age)

➢ Purpose: These lines print the values of name and age in a formatted way.

➢ String Formatting:
✓ %s is a placeholder for a string (in this case, name).
✓ %d is a placeholder for an integer (in this case, age).
✓ The % operator is used to insert the values of the variables into the
placeholders.
P. 18 of 38

3. return Statement
return

➢ Purpose: The return statement is used to terminate the execution of the


function and optionally return a value to the caller.

➢ In this case, the return statement is used without any value, meaning the
function doesn't return anything explicitly.

➢ Why Use return Here?


✓ This is optional in Python if no value needs to be returned.
✓ The function would still work the same way, even if the return
statement is omitted.

4. Function Invocation
printInfo(age=28, name="Kimkim")

➢ The printInfo function is called with the arguments age=28 and


name="Kimkim".

➢ Keyword Arguments:
✓ The arguments are passed as keyword arguments (age=28,
name="Kimkim"), meaning the values are explicitly associated with
the parameter names in the function.
✓ This allows arguments to be passed in any order, as long as the
parameter names are specified.

➢ Inside the function:


✓ name = "Kimkim" and age = 28 are assigned.
✓ The function executes the print statements, displaying the values of
name and age.
P. 19 of 38

Default Arguments

A default argument is an argument that assumes a default value just in case it is not
provided in the function call for that argument.

The example below gives an idea on how default arguments are being used. It
demonstrates the way to print out default age if it is not provided.

Try script defaultFunc.py as shown below:

#defaultFunc.py

# Function definition is here


def printInfo( name, age = 32 ):
print ("My name is : %s" % name)
print ("And my age is : %d" % age)
return

# Now you can call printInfo function


printInfo( age=26, name="Kimkim" )
print()
printInfo( name="Bibi" )

ch = input('')
P. 20 of 38

This is the output:


P. 21 of 38

Elaboration of defaultFunc.py
1. Function Definition
def printInfo(name, age=32):

➢ Function Name: printInfo


✓ This is the name of the function. It is used to reference and call the
function later.

➢ Parameters:
✓ name: This is a required parameter. You must provide a value for
name when calling the function.
✓ age: This is an optional parameter with a default value of 32.
 If no value is provided for age when the function is called, it
will default to 32.

2. Printing Statements
print("My name is : %s" % name)
print("And my age is : %d" % age)

➢ Purpose: These lines print the values of name and age in a formatted way.
➢ String Formatting:
✓ %s is a placeholder for a string (in this case, name).
✓ %d is a placeholder for an integer (in this case, age).
✓ The % operator is used to insert the values of the variables into the
placeholders.

3. return Statement
return

➢ Purpose: The return statement is used to terminate the execution of the


function and optionally return a value to the caller.
➢ In this case, the return statement is used without any value, meaning the
function doesn't return anything explicitly.
P. 22 of 38

4. First Function Call


printInfo(age=26, name="Kimkim")

➢ The printInfo function is called with the keyword arguments age=26 and
name="Kimkim".

➢ Keyword Arguments:
✓ The arguments are passed as keyword arguments, explicitly
associating the values with the parameter names in the function.
✓ This allows the arguments to be passed in any order, as long as the
parameter names are specified.

➢ Inside the function:


✓ name = "Kimkim" and age = 26 are assigned.
✓ The function executes the print statements, displaying the values of
name and age.

5. Second Function Call


printInfo(name="Bibi")

➢ The printInfo function is called with only one argument, name="Bibi". The age
parameter is not provided.

➢ Default Parameter Value:


✓ Since age is not provided, the function uses the default value of 32.
✓ Inside the function:
 name = "Bibi" and age = 32 are assigned.
 The function executes the print statements, displaying the
values of name and age.
P. 23 of 38

Important Notes
1. Order of Parameters:
➢ When using positional arguments (e.g., printInfo("Kimkim",
26)), the order of the arguments must match the order of the
parameters.

➢ When using keyword arguments, the order doesn’t matter.

2. Combining Required and Optional Parameters:


➢ Required parameters (like name) must always come before optional
parameters (like age) in the function definition.

➢ This ensures that Python can correctly interpret the arguments when
the function is called.
P. 24 of 38

More about return statement

Return values in list type

Notice that the return statement can return data in list type for caller to use in
future.

Let’s take a look at program, defaultFuncModified1.py:

#defaultFuncModified1.py

# Function definition is here


def printInfo(name, age=32):
print("My name is : %s" % name)
print("And my age is : %d" % age)

# Return the data as a list


return [name, age]

# Now you can call printInfo function


# Be aware that k is a variable in list type
k = printInfo(age=26, name="Kimkim")

# Print out the items in k list


print("\nPrint out the items in k list:")
for i in k:
print(i)

print()
printInfo(name="Bibi")

ch = input('')
P. 25 of 38

This is the output you would see:


P. 26 of 38

Elaboration of defaultFuncModified1.py
1. Function Definition
def printInfo(name, age=32):

➢ Function Name: printInfo


✓ This is the name of the function. It is used to reference and call the
function later.

➢ Parameters:
✓ name: This is a required parameter. A value must be provided for
name when calling the function.
✓ age: This is an optional parameter with a default value of 32.
 If no value is provided for age when the function is called, it
will default to 32.

2. Printing Statements Inside the Function


print("My name is : %s" % name)
print("And my age is : %d" % age)

➢ These lines display the values of name and age in a formatted way.
➢ String Formatting:
✓ %s is a placeholder for a string (in this case, name).
✓ %d is a placeholder for an integer (in this case, age).
✓ The % operator is used to substitute the values of the variables into
the placeholders.

3. Returning a List
return [name, age]

➢ Purpose: The function returns the name and age values as a list.
➢ A list is a mutable data structure in Python, enclosed in square brackets [],
that can hold multiple values.
➢ Example:
✓ If name = "Kimkim" and age = 26, the function will return:
["Kimkim", 26]
P. 27 of 38

4. First Function Call


k = printInfo(age=26, name="Kimkim")

➢ Keyword Arguments:
✓ The function is called with the keyword arguments age=26 and
name="Kimkim".
✓ Inside the function:
 name = "Kimkim" and age = 26 are assigned.
 The print statements inside the function display the values of
name and age.

➢ Return Value:
✓ The function returns the list ["Kimkim", 26].
✓ This list is assigned to the variable k.

5. Iterating Over the List


print("\nPrint out the items in k list:")
for i in k:
print(i)

➢ Purpose: This code iterates over the elements of the list k and prints each
element on a new line.
➢ Iterating Over k:
✓ k contains the list ["Kimkim", 26].
✓ The for loop iterates over each element in k:
 On the first iteration, i = "Kimkim".
 On the second iteration, i = 26.
P. 28 of 38

6. Second Function Call


printInfo(name="Bibi")

➢ Function Call:
✓ The printInfo function is called with only one argument,
name="Bibi". The age parameter is not provided.

➢ Default Parameter Value:


✓ Since age is not provided, the function uses the default value of 32.
✓ Inside the function:
 name = "Bibi" and age = 32 are assigned.
 The print statements inside the function display the values of
name and age.
P. 29 of 38

You may also get reference to program code defaultFuncModified2.py as shown


below for further elaboration.

defaultFuncModified2.py

#defaultFuncModified2.py

# Function definition is here


def printInfo( name, age = 32 ):
#print("My name is : %s" % name)
#print("And my age is : %d" % age)
return [name, age]

# Now you can call printinfo function


k=printInfo( age=26, name="Kimkim" )

# Print out the data by using index of a list


print("Print out data by using index of a list:\n")
print("My name is : %s" % k[0])
print("And my age is : %d" % k[1])
print()
#printinfo( name="Bibi" )

ch = input('')

Here is the output of the above program:


P. 30 of 38

Elaboration of defaultFuncModified2.py
1. Function Definition
def printInfo(name, age=32):
#print("My name is : %s" % name)
#print("And my age is : %d" % age)
return [name, age]

➢ Function Name: printInfo


✓ This is the name of the function. It is used to reference and call the
function later.

➢ Parameters:
✓ name: This is a required parameter. A value must be provided for
name when calling the function.
✓ age: This is an optional parameter with a default value of 32.
 If no value is provided for age when the function is called, it
defaults to 32.

➢ Return Statement:
return [name, age]

✓ The function returns a list containing the name and age.


✓ Lists are mutable data structures in Python, enclosed in square
brackets [], and can hold multiple values.
✓ Example:
 If name = "Kimkim" and age = 26, the function will return:
["Kimkim", 26]
P. 31 of 38

2. First Function Call


k = printInfo(age=26, name="Kimkim")
➢ The printInfo function is called with the keyword arguments age=26 and
name="Kimkim".

➢ Keyword Arguments:
✓ The arguments are passed as keyword arguments, explicitly
associating the values with the corresponding parameter names.
✓ This allows the arguments to be passed in any order.

➢ Inside the Function:


✓ name = "Kimkim" and age = 26 are assigned.
✓ The function returns the list ["Kimkim", 26].

➢ Return Value:
✓ The list ["Kimkim", 26] is assigned to the variable k.
P. 32 of 38

3. Accessing List Elements by Index


print("Print out data by using index of a list:\n")
print("My name is : %s" % k[0])
print("And my age is : %d" % k[1])

➢ Purpose: This block of code prints the values stored in the list k by accessing
its elements using their indices.

➢ Indexing in Lists:
✓ Lists are zero-indexed, meaning:
 The first element is accessed using index 0.
 The second element is accessed using index 1.

➢ Accessing the List:


✓ k[0]: Accesses the first element in the list, which is the name
("Kimkim").
✓ k[1]: Accesses the second element in the list, which is the age (26).

➢ String Formatting:
✓ %s is a placeholder for a string (in this case, k[0]).
✓ %d is a placeholder for an integer (in this case, k[1]).
✓ The % operator substitutes the values into the placeholders
dynamically.

➢ Output:
✓ The print statements display:

Print out data by using index of a list:

My name is : Kimkim
And my age is : 26
P. 33 of 38

Looping and Function


Self-defined temperature conversion function
The following code listing will convert several items in for loop from Celcius to
Fahrenheit.

temperatureFunction.py

def toFah(theInput):
temp=(theInput*9/5)+32

return temp

##print out result here


print("The conversion report:")

for x in (6.52, 12.19, 30.4, 60):


k = toFah(x)

print(x, "\t->", end="")


print("%8.2f" %k)

ch = input("")

See the output below for reference:


P. 34 of 38

Elaboration of temperatureFunction.py

1. Function Definition
def toFah(theInput):
temp = (theInput * 9 / 5) + 32
return temp

➢ Function Name: toFah


✓ This function is used to convert a temperature from Celsius to
Fahrenheit.

➢ Parameter:
✓ theInput: This is the temperature in Celsius that is passed to the
function when it is called.

➢ Formula for Conversion:


temp = (theInput * 9 / 5) + 32

✓ This formula converts Celsius to Fahrenheit.


✓ The calculation involves:
1. Multiplying the Celsius temperature by 9/5.
2. Adding 32 to the result.

➢ Return Value:
return temp

✓ The function returns the converted Fahrenheit temperature (temp).

2. Printing a Header
➢ print("The conversion report:")

✓ This line outputs a header ("The conversion report:") to indicate that a list of
temperature conversions will be displayed.
P. 35 of 38

3. Iterating Over a Tuple of Temperatures


for x in (6.52, 12.19, 30.4, 60):
k = toFah(x)
print(x, "\t->", end="")
print("%8.2f" % k)

a. Iterating Over the Tuple


for x in (6.52, 12.19, 30.4, 60):

➢ A for loop is used to iterate over the tuple (6.52, 12.19, 30.4, 60).
➢ Tuple: A tuple is an immutable data structure in Python, here containing a list
of Celsius temperatures to be converted.
➢ The variable x holds each temperature in Celsius as the loop iterates.

b. Calling the Function


k = toFah(x)

➢ For each temperature x in Celsius, the toFah function is called.


➢ The result of the conversion (the temperature in Fahrenheit) is stored in the
variable k.

c. Printing the Conversion


print(x, "\t->", end="")
print("%8.2f" % k)

➢ First Print Statement:


print(x, "\t->", end="")

✓ This prints the current Celsius temperature (x) followed by a tab (\t)
and the arrow symbol (->).
✓ The end="" argument prevents the cursor from moving to the next
line, so the subsequent print statement continues on the same line.
P. 36 of 38

➢ Second Print Statement:


print("%8.2f" % k)

✓ This prints the Fahrenheit temperature (k) formatted to 8 characters


wide with 2 decimal places:

 %8.2f specifies:
 8: The total width of the field, including the number,
decimal point, and decimal places (adds padding if
necessary).
 .2: The number of digits after the decimal point (2 in
this case).
 f: The format specifier for a floating-point number.

➢ Example Output:
✓ If x = 6.52 and k = 43.736, the output will be:

6.52 -> 43.74


P. 37 of 38

Classwork 2
Sunlight Café is a newly opened café which provides Buffet with $138 per person.
With a view to promote the company, the boss has launched a “Buy 1 get 1 free”
promotion on buffet. That means if 2 persons come, the café will only charge the
price of 1 person. On top of this, there is a 13% service charge on the net price (net
price is the price charge without service charge).

The boss would like to have a program (Buffet – function and list.py) that can show
the net price, service charge fee and final price once after he inputs the number of
persons attend the buffet.

Within the program, add a self-defined function called buffetPrice(para1). This


function should reutrn the calculated answer in a list structure.

Here is the output of the program:


The program should first prompt user to input the number of persons going to attend
the buffet:
P. 38 of 38

After user input the number of persons, the program should show all the results to
user. That is:

You might also like