![]() |
![]() |
|
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
Due Apr.02 (Wed) 10:00; submit a file hw04.py (written in JES) to hw04-dropbox and turn in a hardcopy in class Apr.02.
(6pts)
In lab, we wrote a Turtle function
Your task is to write
(8pts)
Write the function
Account setup: Be sure to create/use a codingbat account using yourRadfordUserName@radford.edu, and that you have set, at some point, prefs > Share To is set to ibarland@radford.edu. (If you re-register sharing with me, it'll say you're already sharing, so no harm done.)
When you're done, paste your code into your hw04.py file. Include a comment immediately preceding the function, reporting “ codingbat test-cases didn't pass”, where the blank is filled in with a number (hopefully, the number will be 0!).
You will want to include, in the part you type in to codingbat.com,
our definition of
# substring : string, int, int → string # Given some text, a start-index, and a stop-index, # return the characters of `txt` from `start` up until (but not including) `stop`. # Indices are 0-based. # Examples: substring( "radford", 3, 6) = "for" # substring( "radford", 0, 3) = "rad" # def substring(txt, start, stop): return txt[start:stop] |
Paste your code (and report # test cases that don't pass), as in the previous problem.
Hint: Here is an example of how to get a random number in the range 0 up to (but not including) 4; try typing it in JES's interactions (lower) pane:In your program-file itself (the top page), have the
import random random.randrange(4) random.randrange(4) random.randrange(4)import statement just once, up near the top of your file.Beware:
randrange is not likerange , despite the similar names (one returns a single integer; the other returns an entire list-of-ints that can be looped over). They are no more similar that Ford and Radford.
Clearly, this function should work by calling
Calling this function
Hint: If you call your function to draw a large number of sides, you'll want to pass it a small number for the size, so that it stays on the screen. Turtles get a bit wonky if they run into the edge of the screen.
Extra: For a little further extra credit: have this function take in not the length-of-each-side, but rather the desired diameter (of the circumscribing circle). This will require some geometry/trig, to figure out how long a side should be based on the diameter!
1Some people may have written a version which drew a zag-shape three times longer than the requested size, end-to-end. If that was you, you'll want to fix it. ↩
2If this is a level-2 zag, we might say that the original was a level-1 zag. After you complete this task, it's left as a fun self-task, to draw a level-3 zag. ↩
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Apr.02 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |