Chapter 6 Exercises¶
There are errors in the indention in the following code. Fix it to work correctly without errors.
The lines following the defintion of the square procedure needed to be indented by 4 spaces as shown below.
Fix the errors so it runs and returns the perimeter of a rectangle.
Indentation needs to be fixed and there needs to be a colon after the function name.
return
should be lowercase and you should return the variable in the function not the function name.There are 2 syntax errors in the following code. Fix the errors so that it runs.
There must be a
:
at the end of the procedure definition. You also have to pass malik to the square procedure as shown below.Fix the errors so the code runs and returns the area of a square.
‘’def’’ should be lowercase. The call to the function should occur after the function is defined.
The following code has 4 syntax errors. Fix the errors so that the code runs.
You must change the
Malik
tomalik
on the calls to square. Remember that Python is case sensitive.Change the code to take 3 parameters, a turtle, a size that tells it how far to go, and an angle it tells the turtle to turn.
Add both the
distance
andangle
parameters to the function definition.The following code has three lines that need to be changed. Fix the code to run correctly.
You have to pass the turtle first and then the size separated by a
,
.Fix the errors so it prints
"My name is John and I am 18 years old"
.It’s a function so you need to return not print. Also, fix the errors in returning the values. Then in the call to the function, make sure the string is the first argument and the age the second.
Change the square procedure below to take a size parameter and have the turtle go forward by the specified size each time.
Add the
size
parameter to the procedure defintion and be sure to add a value for the size when you call the procedure as well.Change the code so the function takes parameters for the base and height of the triangle. Then, write code to call the function and print the result.
Add the parameters for
base
andheight
and call the function inside the print statement.Change the code below to create a function that calculates the cost of a trip. It should take the
miles
,milesPerGallon
, andpricePerGallon
as parameters and should return the cost of the trip.Add a function definition that takes as parameters
miles
,milesPerGallon
, andpricePerGallon
. Don’t forget to call the function to test it.Fix the errors in the procedure and call it.
The procedure has to be defined before you can call it. Also, in the procedure, make sure the turtle methods are using the
distance
andangle
parameters.Change the code below to create a function to return the number of miles you can drive. It will take as input (parameters) the
tankCapacity
,theAmountLeft
, and themilesPerGallon
.Add a function definition that takes as parameters
tankCapacity
,``amountLeft``, andmilesPerGallon
. Be sure to call the function to test it.Complete and change the code to be a function with 2 parameters that returns the time taken to travel and call the function
Take
speed
anddistance
as parameters and returndistance / speed
. When calling the function, make sure the arguments are in the right order.Create a procedure to draw a rectangle and call it. Be sure to take the
width
andheight
of the rectangle as input to the procedure.Create the procedure and be sure to call it to test it.
Create a procedure that takes 2 parameters, a string that you get from a user input and an int. Make the procedure print the string the number of times the int parameter gives and call the procedure.
Procedure means you want to print not return. Have a string and an int parameter and print the product of the two. When calling the procedure, first get the user input and then pass that input as an argument.
Create a procedure to draw a triangle and call it. Be sure to take the length of each side of the triangle as input to the procedure.
Create the procedure and be sure to call it to test it.
Create a procedure that takes 7 paramters (turtle, distance, angle, and 4 color strings) and call the procedure to draw a square in 4 different colors.
Take a parameter for the turtle, distance, and the 4 colors. When you call the procedure make sure the arguments are in the right order.
Write the code below to create a procedure that prints a mad lib. You can ask the user for input and then pass that input into the procedure.
Create the procedure and be sure to call it to test it.
Write a function that takes the current hour, current minute, an int to be added to the current hour, and an int to be added to the current minute, and return a string with the new hour and minute (standard 12 hour time; if minutes exceed 60, it should go to the hour) and call the function.
Make sure the function takes in the 4 parameters. Use the mod function to make sure minutes and hours don’t exceed 60 and 12, respectively. The total minutes minus the remaining minutes (from the mod function) gives the number of hours made from the extra minutes.