Introduction to Computer Programming: Lab 0

Goals

experience a few different systems for creating and interacting with programs

experiment using IDLE

practice using very basic sequential Python programming including:

numbers and simple arithmetic operations

strings and concatenation

variables and assignment

integer division (//) and remainder (%) operators

prepare for the first programming assignment.

Instructions

Work on this lab with your partner (or on your own if you have not been assigned one).

Unless otherwise indicated, use only the Python operations and commands we have described thus far in class.

Show your work for a given problem to the instructor or one of the lab assistants before moving on to the next problem, but do not get too bogged down on any one problem.

In the common case, you will not complete the exercises during our scheduled lab. It is then up to you to complete the lab on your own time.

Programming with Blocks

Try this simple block-programming puzzle to get used to how blocks are manipulated. It should only take a few minutes to complete. (It is not trying to teach you anything interesting about animal biology!)

Now try these maze-navigation puzzles. The blocks do all the hard work for you; you can see what the solution would look like in another programming language (JavaScript) when you complete each level. You should not worry about the JavaScript syntax (that is another course entirely) it is there to prove to you that the abstract, graphical blockly system is equivalent to more traditional text-based code. See how many levels you can complete in 15 minutes or so. For extra credit, when time permits, try the 10th and final maze-navigation puzzle.

import turtle
window = turtle.Screen()
mike = turtle.Turtle()
window.bgcolor('yellow') # change the background color as you see fit
mike.shape('square') # change my shape as you see fit
for _ in range(16): # you can change the number of iterations
# you can reorder these commands and change their parameters as you see fit
mike.color('green')
mike.speed(1)
mike.penup()
mike.stamp()
mike.forward(17)
mike.right(30)
mike.pendown()
mike.left(45)
mike.forward(34)
window.mainloop()

You should feel comfortable about why some very similar looking (to humans!) expressions produced different results. Now write three more examples using the type function that produce three different results distinct from any of the above examples.

Inputting numbers. Try the three examples below. Be able to explain why the result produced is different in each case.

Write a similarly structured program that achieves the inverse, namely converts Fahrenheit to Celsius.

Googol (the number, not the search engine). Complete this program that computes one googol (one followed by one-hundred zeroes) using only numbers, +, -, *, / and the ability to define variables: using as few characters as possible. (Obviously, you can do it with 101 characters. See how much smaller you can make your program.)

def googol():
print('one googol =', ...)

Think about the power of symbolic abstraction and how it applies here.

String length. Write a function that asks the user to enter some text and then reports how many characters were entered. Example:

>>> say_length()
Enter some text: now is the time
That phrase has 15 characters in it.

String concatenation. Write a function that asks the user to enter their first name and then their last name. Use the concatenation of those two strings to report back how many letters are in their name. (Careful: the space displayed between the first and last name should not count toward the length of the full name.) Example:

Write a program that asks the user for a total number of hours, and converts this to the corresponding number of weeks, days, and hours. Hint: the remainder (%) and integer division (//) operators might come in handy here. Examples: