CSCI 110 / Spring 2012
Survey of Computer Science
Lab 3
Due at the end of today's class, except by permission.
Problem #1.
Write a Python program, named flower.py, that uses a turtle to draw a flower. The program should ask the user for the number of petals and should use a while loop to draw that number of petals. The petals should be equally spaced around a circle. In other words, if the user enters 3, the program should draw 3 petals, spaced at intervals of 360/3 = 120 degrees. lastly, your program should draw a stem for the flower to complete the picture.
You should use the following code to draw a single petal:
my_turtle.forward(100) my_turtle.left(45) my_turtle.forward(100) my_turtle.left(135) my_turtle.forward(100) my_turtle.left(45) my_turtle.forward(100) my_turtle.left(135)
Below is a picture of a flower with 6 petals.
You may use the program skeleton given below to help you get started:
from turtle import Turtle, exitonclick petals = input("How many petals would you like? ") turn = 360/petals # Amount to turn after drawing each petal count = 0 # A variable to count how many petals have been drawn so far my_turtle = Turtle( ) # Add your own code here to write a while loop to draw the petals. # The loop should draw a single petal each time it is executed. # It should then turn the turtle the correct amount to be ready to draw the next petal. # it should also increment count by 1. # The loop should continue as long as count is less than the desired number of petals
Problem #2.
In this problem you will write a program that simulates a turtle's random walk inside an imaginary box, counting the turtles steps as it moves around. Your program should stop when the turtle eventually goes outside the imaginary box, then print out how many steps the turtle took. Name your program walk.py.
The imaginary box is N pixels on a side centered at coordinates x=0, y=0, where N is some number chosen by the user. So if the user enters 100, the bottom left corner of the box would be at x=-50, y=-50, and the top right corner would be at x=+50, y=+50.
The turtle starts at the center of the box (coordinates x=0, y=0) then repeatedly takes steps. To make the turtle take a step, your program should pick a random distance between 1 and 10 pixels and a random angle between -90 and +90 degrees, then move the turtle forward that distance and turn the turtle left that number of degrees. The turtle should keep walking forward and turning so long as it stays inside the imaginary box.
Your program should have roughly the following structure:
Your program will need to have a variable that is incremented on each iteration of the loop to keep track of how many steps have been taken.
Hints:
import random from turtle import Turtle, exitonclick
my_turtle.speed(5) # increase the turtle's speed a bit; 5 is medium; 11 is the fastest # go to the bottom left corner of the imaginary box my_turtle.penup() my_turtle.goto(-N/2, -N/2) # now draw the imaginary box my_turtle.pendown() my_turtle.forward(N) my_turtle.left(90) my_turtle.forward(N) my_turtle.left(90) my_turtle.forward(N) my_turtle.left(90) my_turtle.forward(N) my_turtle.left(90) # now position the turtle in the center my_turtle.penup() my_turtle.goto(0, 0) my_turtle.pendown()
The following is an example of the output of running the program. Below the output is the image it draws.
Sample run:
What size box would you like? 200 The turtle took 108 steps before leaving the box.
Problem #3.
Entering input using the keyboard is tiring, especially when the input consists of a large amount of data. Instead, it is common to store data in a data file, then have the program read some of its inputs from the data file instead of from the keyboard.
In this problem you will a program, called stats.py, that calculates the average of a list of decimal (float) numbers, where the input numbers are stored in a data file chosen by the user. The data file is just a text file with one decimal (float) number on each line. Your program should print the result with at most two decimal places.
Here are some examples of the output of running the program:
Sample run 1:
Please enter the name of a data file: temperature.txt The data file temperature.txt contains 9 numbers whose average is 35.66.
Sample run 2:
Please enter the name of a data file: salary.txt The data file salary.txt contains 34 numbers whose average is 43541.18.
To run your program you will need some data files. You can create your own data files with Notepad or Microsoft Word (make sure you save as "plain text" ".txt" file, not a ".doc" or ".docx" file), or even using Python's IDLE editor (just save the data file with a ".txt" ending instead of ".py"). You can also download the following two files, which were used in the above example runs. NOTE: You must put the data files in the same directory where your python program is saved.
Below is an outline of what your program should look like.
# Ask the user for the name of a data file. # Open that data file. # Initialize one variable to store a count of how many numbers have been read so # far, and another variable to store a sum total of all the numbers that have been # read so far. # Read the first line of the file. # So long as the end of the file has not been reached, repeat the following: # Convert the line into a decimal (float) number. # Update the variables for the counter and total. # Read the next line of the file. # Calculate the average using your variables for count and total. # Print out a summary that includes the name of the data file, how many # numbers were read from that data file, and what their average is. The average # should be printed with at most two decimal places.
Opening and reading data files in Python: Here is one way for a Python program to open a data file and read inputs from it. Suppose the variable file_name contains the name of the data file as entered by the user (e.g. the string "temperatures.txt"). Then
data_file = open(file_name)
will open that file and assign the resulting File object to the variable data_file. You can then write
line = data_file.read_line()
to read one line of the file and assign the resulting string to the line variable. If the end of the file has been reached, then line will be an empty, zero-length string. So your program will use a while loop to repeatedly read lines of the file in turn until an empty string is encountered.
Bonus: The program above expects the data file to contain one decimal (float) number on each line and no blank lines or comments. A better program would not be so picky. That way, the data file can have comments explaining what the data is, were it came from, etc. Change the while loop in your program so that any lines of the data file that are blank or that begin with the letter '#' are ignored. A blank line is represented by "\n", a string containing a single newline character. Here is a data file you can use to test your modified program:
What To Turn In.
Submit your three program files (flower.py, walk.py, and stats.py) to Moodle. Be sure that your name is in the program prologue's comment section of EACH program.
Reminder.
Be sure to save a copy of each program on your P:\ drive or gmail, etc.
You are responsible for keeping a copy of every program until the graded assignment is handed back.