Workshop7 2
Workshop7 2
A python function “avg” returns the average of three values, as shown below:
def avg (num1, num2, num3):
return (num1 + num2 + num3) / 3.0
n1 = 37, n2 = 108, n3 = 67
Define the function and variable declarations given above in IDLE shell and
execute the following expressions. Which of the statements are valid?
Note down the response to each. Do they differ from what you would expect?
Part 2
Define a function:
(A) types( ) that prints a given value both as a float and an integer
(B) squared( ) that take an integer and returns the value squared.
(D) hello_world( ) that takes a parameter name and displays the following output
to the console: "Hello World, my name is name".
(E) print_ast( ) that takes an integer value n and a string value symbol, with a
default value of "*". This character should be printed n times to the console.
(F) improved_average( ) that takes five integer parameters. It should return the
mode, median and mean values of the numbers passed to the function.
(G) either_side( ) which when passed an integer value also prints the values which
are one less and one more than that value e.g.
"You typed 4, one less than 4 is 3, one more than 4 is 5"
Part 3
1. Create a function that prompts the user for two integer values and displays the
results of the first number divided by the second to two decimal places.
5. Use a looping statement to take user’s choice to continue for the above program.
*Used the looping statement at qno.4*
Part 4 (Optional)
You will need to understand control structures to complete the following questions.
Therefore, you should carry out some independent research before attempting this.
However, it will also be covered next week in class.
1. Create a function multiplication_table( ). It should take a single parameter n,
which determines the size of the grid to be output e.g.
multiplication_table(10)