Вы находитесь на странице: 1из 3

Computer Science I

ShapyTurtle

2161
Lab
Strings 09,,20,6

Problem

ShapyTurtle is an interpreter for a 'shorthand language' that allows us to express turtle


programs compactly as a string. This interpreter is defined by a function that
takes a program given as a string argument.

Command
<#
>#
S#
Tit
C#
F#
B#
U
D
R#1,#w
P#s,#1
G#x,#y
z#

Table 1: ShapyTurtle Commands


Effect
Turn left # of degrees.
Turn right # of degrees.
Make a square with side length #.
triang!e \vith side length #.
#.
with
radius
Make a circle
Move forward distance #.
Move backward distance #.
Pick the pen up.
Put the pen down.
Makes a rectangle with length #1 and width #w.
Draw as turn, length, turn, width, turn, ...
Makes a polygon with s sides with side length #1.
Draw as turn, length, turn, length, turn, ...
Goto location #x, #y.
Changes the color of the turtle pen based on #.
0: red -r---&.(v-014.0
1: blue
2: green
3: yellow
4: brown
other: black

The ShapyTurtle instructions in Table 1 are ones that cause the turtle to draw or change
its state. The interpreter of ShapyTurtle programs first clears the canvas and places the
turtle starting at the origin, facing East (0 to the horizontal) with the turtle pen down
before executing the program string.
All ShapyTurtle instructions should begin at the current turtle orientation and return to
that orientation; except for the cases of turns, <and >.
The drawing of the shapes should be counterclockwise. For instance, to make a square
you will do left, forward, left, forward, left, forward, left, forward.

Implementation (70%)

4.1

Details

You are required to implement all of the ShapyTurtle commands in Table

1.

For this lab, you are required to create functions for processing each of the ShapyTurtle
commands. When you come across a ShapyTurtle command it should call a function to
process that command and move beyond the characters needed for that command so it
can process the next command.
The ShapyTurtle command function should handle errors. If an error occurs, your function
should return None. For example, processC( "CC100" )should return None because C
is followed by another C and not a number.
Your ShapyTurtle processing function will check only the first character of the input. It
will send the input to the proper ShapyTurtle command function. If the character is not
a valid ShapyTurtle command, it will print and error and end the function. It will not
change the string other than moving beyond the command character. If a ShapyTurtle
command returns None, an error, the program will print the text it tried to proeess and
quit.
Your main function will ask the user to input a string containing ShapyTurtle commands.
It will then call your ShapyTurtle processing function with that command. You can assume
that only one ShapyTurtle command can be entered per execution of your program.
One long function that does everything will result in a deduction of 50% of the Implementation points. You are allowed to use iteration, slicing, and indexing of strings.
4.2

Handy Tools

This section will outline some handy tools that can be used for this lab.
It is important for this assignment to be able to check if a character is a numeric
1.
digit. Python has a function to do that, and you call it like this: st.isdigit(),
where st is a reference to a character string. It will return a True/False answer;
True if it is a digit, False otherwise.
Multiple value return from a function. Python can return multiple values from a
2.
function using tuples (these will be explained more in week 6).
Example:
def foo(x):

y = x + 1
x = x - 1
return (x ,y)
a = 0
(a, b) = foo(a)
print(a)
print(b)

Notice the return values are wrapped in parentheses and that pattern is matched
when the function is called.

Grading Rubric

The 70% lab portion of your assignment will be graded following this rubric:

30%: Implements the functions for processing each of the ShapyTurtle commands.
Each of the functions handles errors properly.

10%: A main function that prompts the user for a string to process and calls a
function to process that input using ShapyTurtle commands.

25%: A function that processes the input text and calls the proper ShapyTurtle
command function. Errors are handled properly, such as invalid command characters.

5%: style. The code follows the style guidelines on the course web site, including
- -(1(xstrings. for cosh function with type, specifications and author's fall name- iii the
file comment block at the top of the file.

Submission

Submit your work in shapy_turtle.py and upload the file to the correct MyCourses
dropbox. Naming the file something other than requested will result in a 20% reduction in
your lab grade. Naming your file differently makes your lab harder to grade via automated
testing.

Вам также может понравиться