< Back

Lab 11: Images


Practice Problem 1

Write a function called make_excited(phrase), which takes a string as a parameter. Your function should print the phrase that was passed as a parameter with an exclaimation point at the end.

Example

>>> make_excited("Hi")
Hi!
>>> make_excited("INQ 241A")
INQ 241A!

Hint

A function always begins with the def keyword. You then provide some name for the function (make_excited in this case). Finally, you define any parameters you want between the parenthesis (phrase in this activity). Remember, this just defines a variable you can use within the function, and can be called anything you want. Follow the naming convention here, though. Don't forget the : at the end.

All lines that belong to the function must be indented one tab from the left margin. This signifies that you are including that line of code with the function.

The + operator is defined for strings, and allows you to join two strings together. Use this to add an exclaimation point.


Practice Problem 2

Write a function called cube_surface_area(side_length), which takes a single integer as a parameter. Your function should print what the area is of a cube with those specified dimensions. Remember that a cube is 6 squares, all of which have dimensions of \(side\_length \times side\_length\).

Example

>>> cube_surface_area(1)
6
>>> cube_surface_area(2)
24
>>> cube_surface_area(3)
54

Hint

Use a variable here to simplify your expressions. Remember, a variable is just a name that you are giving a value. The syntax for that is variable = value, where variable is just some name you are giving to value, and value is some python statement which evaluates to a value.

The * operator is defined on integers as the multiplication of those integers. They even follow the order of operations, if necessary.


Lines

In a file called lines.py, write a function called draw_lines(width, height). This function should create a new picture of the specified size. It should then add red pixels into the image, to make the following pattern.

Example

Hint

Use the makeEmptyPicture(width, height) function to create a picture in JES. You can use the show function to display the image to check to make sure it works.

You can use the makeColor(red, green, blue) to create an arbitrary color.

The getPixel(a_picture, x, y) function returns a single pixel from the picture a_picture. The x value specifies a location along the x-axis, while the y value specifies a value along the y-axis. You can use this function to set a color for a specific pixel.

The setColor(pixel, color_value) function can be used to set a specific pixel to a specific color.