Project Writeup
This homework deals with user input and output statements, and of course, turtle graphics.
The first part of the task was to write a program that allowed a user to repeatedly enter a string of characters that correspond to commands for turtle graphics. The user would enter as many strings as desired, and the turtle would continue to draw graphics as long as the user entered strings. As soon as the user wanted to exit the program, hitting the enter key would break the loop and exit the program. The program also asks the user for a filename before entering the string. The program opens the file writes (or stores) the string entered to that file, and then closes the file.
To accomplish this, I copied my turtleUtils.py, linear.py, and radaley_shapes.py
files from the Homework Three Assignment, and put them into one file named follow.py.
(See the Homework Three link for further explanation of these files). First I asked
the user for a filename using a print statement, and then I grabbed the input using
the raw_input() function. I used the file(filename, 'w') function to create the file.
Next, I modifed the
processString function so that it does not move the turtle, it just
draws the shapes (in this case, the shapes would be drawn over each other). Then
I added the following characters to my drawShapes function:
'u' is up()
'd' is down()
'f' is forward(10)
'b' is backward(10)
'r' is right(30)
'l' is left(30)
I added a print statement with the above definitions so that the user would know
the appropriate characters to enter into the string.
To allow the user to continuously enter strings of commands, I used a while loop,
and began it with the statement "while True: ", which will continue the loop until
a statement is encountered that is false. However, the way that my drawShapes function
is set up, a false statement is never created. In otherwords, even if the character
entered by the user is not defined, the final portion of the conditional function,
the else statement, will print "Sorry, Invalid Selection" and the processString
function will move on to the next character in the string. I had a print statement that
asked the user of the program to enter a string of variables, and I defined all
the variables in a print statement to let the user know the actions of each specific
character. I used the raw_input() function to grab the string entered by the user,
and entered it into a variable, commandString. The processString function was called
with commandString entered as the variable, and this caused the function to evaluate
the string entered by the user and have the turtle execute the appropriate graphics.
I also used the ".write(commandString)" function to write the string inputs by the user
into the file he or she created at the beginning of the program.
To break out of the while loop, I used a conditional if statement where if the user
did not input anything, the "break" command would end the program. After the user
breaks out of the program, the ".close()" function to close the file. Using the program
myself, I was able to create the image below:
Just in case anybody was wondering, the string that I entered and saved to a file
named scene4A.txt was:
fsrrrfftrrrfffhrrrffffsrrrffffftrrrffffffhrrrfffffffsrrrfffffffftrrrfffffffffhrrr
ffffffffffsrrrffffffffffftrrrffffffffffffrrrhfffffffffffffsrrrfffffffffffffftrrrf
ffffffffffffffhrrrffffffffffffffffsrrrffffffff
The next task was to create a program that would ask the user to enter a filename, and then have the program open the file, read the string contained in the file, and then send the string to the processString function for it to be evaluated and drawn with turtle graphics.
I again used a print statement to ask the user to enter a filename, the raw_input()
function to grab the input, and entered what the user typed into the variable
"filename." To open a file, I created an object using "file(filename, mode)", where
mode refers to 'w' if one were to write in the file, and 'r' if one were to read the
file. I created a variable "fb" to hold the file object described in the previously
(I actually used 'filename' because that was the variable for the user input, and
for mode I used 'r' because the task is to read the file), and then used the readline()
function to read "fb", which I then entered into a variable "aString." The processString
was called with the variable "aString", which converted the string into turtle graphics
commands. I used a try statemet to send an error message to the user if a filename was entered that did not exist.
After creating this program, I created a blank text file that had a string
of commands from the modified drawShapes function. I used the program created to
read the string I entered in the file, which created image below:
Again, just in case anybody was wondering, the string that I entered and saved to a file
named scene4B.txt was:
ffllfffrrrrfffrrrrfffbbbllllllffrrfffllllfffllllfffbbbrrrrrrffffllfffrrrrfffrrrr
fffbbbllllllffrrfffllllfffllllfffbbbrrrrrrffffllfffrrrrfffrrrrfffbbbllllllffrrff
fllllfffllllfffbbbrrrrrrff
The extension for this project requires importing the string module. The string
module allows me to use the replace() function. The replace() function is defined
by the arguments base, symbol, and rule. The replace function finds every occurrance of
the string symbol in the string base and replaces it with the string
in rule.
In a koch.py file, I defined the variables base, symbol, rule, and complexity
according to Professor Maxwell's instructions. I ran a for loop that iterated over
complexity (which was a variable assigned with a number), and had the final value
of the base variable (which was repeatedly updated each time the loop iterated on
account of the replace() function) written to a file kochextension.txt. Using my
linearC.py program which opens and reads files, I was able to produce the image
below:
I proceeded to make minor adjustments in reducing the forward(10) command to
forward(5), and changed the complexity variable to 4 (an increase of one iteration).
I also added a random color function to the "f" parameter of the drawShapes function,
but I entered "1.0" in the section for blue so that it would generate a winter-like
snowflake resembling color. This had a surprisingly large effect on the figure:
When I tried to increase in the number of iterations to 5 in the koch.py file, the
figure became to large to fit on the turtle screen.