Question 1: Write a program that uses the function div_with_remainder that was defined in class.
This was the function div_with_remainder:
def div_w_remainder(dividend,divisor):
whole = dividend//divisor
remainder = dividend%divisor
return(whole,remainder)
- Define two global variables: big_total and maximum_share and initialize both of them to 0.
- Write a function called share_items that takes 2 parameter arguments: a total_number_of_items, and the the number_of_people sharing the items. This function should use divide_with_remainder to figure out how much each person gets (whole) and what is left over (remainder). It also should update the 2 global variables as follows: (i) big_total should be the total of all instances of total_number_of_items; and (ii) maximum share should increase by whole if remainder is 0 and increase by whole + 1 if remainder is greater than 0. Basically big_total is the total of things that are being divided over all and maximum_share is the largest amount that one person would receive if all participants shared the items being distributed in a mostly-fair way (some people got one extra when there was a remainder).
- Write a function called question_1() that calls share_items at least 3 times and shows that it works. For example, the following instance of question_1, calls share_items 3 times: the first time with arguments of 10 and 4; the second time with arguments of 20 and 3; and the third time with arguments of 14 and 7. Then it puts big_total and maximum_share in a print statement. Here is what it looks like to call that function and for the result to print out:
>>> question_1()
The total is 44 and the maximum share is 12
>>>
Question 2: Use the turtle module to draw a simple stick figure:
- A google image search illustrates what a stick figure is, in case you are unfamiliar with the term (or want some inspiration).
- Define separate functions for different components of your stick figure. This should make it possible to use the same function over and over to draw multiple stick figures with different attributes. As an example, see the py file (and possibly other programs demonstrated in class and on the class website). You do not have to initially add all the features there, but you should be aware of the possibilities as this will help with later questions in this homework. For example, you can write functions like: draw_left_arm, draw_left_leg, draw_stick_figure. These should all take parameter arguments, so it is possible to vary things about them: their size, their color, etc.
- Note the following:
- circle(radius) will draw a circle with radius radius. It will draw it above the direction that the turtle is facing. This is useful for drawing a round head. Also, the optional argument extent allows you to draw parts of circles (see the make_dog_head function in turtle_dog.py).
- There are various different variables that determine colors:
- fillcolor — sets a color to fill in shapes created between: my_turtle.begin_fill() and my_turtle.end_fill()
- pencolor — sets color of the pen
- bgcolor — sets color of screen
- colormode(255) — sets the color mode as a combination of red, green and blue values, each a number between 0 and 255. If you do not set this, you can use normal color names (blue, black, red, etc.), but there will be fewer colors overall.
- Note the following:
- Remember NOT to name your program file py — if you do, python will not be able to find the turtle module and your program will not work (import turtle will try to load your file instead of the module). Module names, filenames, function names and variable names should have unique referents.
Question 3:
- Modify the program in question 2 (question 2 and 3 do not have to be distinct programs).
- Repeat and/or randomize elements from question 2. The important thing is that the picture should look different each time you run it and there should be multiple components that change.
- The whole picture that you created (as in turtle_dog.py) can repeat with different settings
- Parts of a big picture can repeat with different settings, e.g., the whole picture could represent a scene with different random elements.
- Feel free to use any turtle method you want. Feel free to use other ideas from class:
- The turtle manual can be found at: https://docs.python.org/release/3.5.2/library/turtle.html There are many methods that I have not covered. You are free to use these if you want.
- You should consider using the fuzzy lines (or similar strategies) that I proposed in class to simulate pointalism, i.e., features of the drawing process (not just particular elements) can change.
- Criteria for grading:
- Does your code work without crashing?
- Do you have functions that encapsulate ideas, e.g., draw_left_arm, draw_right_arm, draw_finger, etc.?
- Did you find an elegant way to call the same function in different ways using parameters?
- Are there interesting random variations?
- Is your code easy to run? For example, it would be good to either:
- Have a main function that runs your code. You can call this main function at the end of your .py file.
- Or you could have really good instructions (in comments) explaining how to run the code.
- Do you do anything interesting or creative?
- Did you use any additional interesting command from the turtle manual?
- If you want to print out an image from the turtle program (not required at all). You should do it as follows:
- getcanvas().postscript(file=”your_file_name.eps”)
- ## my_screen should be the name of the screen object and your_file_name is whatever filename you want
- .eps is a type of image file. You should be able to insert .eps files into other documents and manipulate it in at least some graphics editing programs.
- For example, the following image was created using the turtle_dog.py program described below in the extra credit section. The command was: my_screen.getcanvas().postscript(file=”5_random_dogs.eps”)
- getcanvas().postscript(file=”your_file_name.eps”)
- Examples of Turtle attributes that you can randomize:
- fd(distance) ## use a random number instead of a set distance (the range should not take the turtle off the page, unless you want it to — see above)
- lef(degree) ## choose a random number between 0 and 360 (or a smaller range) if you want the turtle to turn in a random direction
- If you use my_screen.colormode(255) — as above, all colors are analyzed as combinations of three numbers representing red, green and yellow, each number is between 0 and 255, there are several different attributes that you can set to a particular color. In each case, it is possible to randomize the values of red, green and yellow, e.g., with random.randint(0,255). Color changing functions include:
- pencolor(200,100,0) — sets pencolor (the line the turtle draws) to a new color in this case a shade of brown/orange
- fillcolor(50,100,200) — sets the color that will be filled in by commands that follow (this color is a shade of blue)
- begin_fill() — start point of some figure that will be filled in
- end_fill() — end point — when this activates, the figure is filled in using fill color
- bgcolor(255,255,0) — sets the background color of the screen, in this case to yellow
- You can randomly move the turtle to any position on the screen using coordinates (if you don’t want a line to be drawn from your current position, remember to use my_turtle.pu() to pick up the penn first.
- setposition(50,50) will move a turtle to a position in which X = 50 and Y = 50, the values can be randomized. The center of the screeen is 0,0 and X and Y values can be positive or negative. In the example program turtle.dog, screen_size_multiplier*screen_width and screen_size_multiplier*screen_height were assumed to be the maximum distances a turtle could start drawing at. Sometimes, with that program, the turtle did draw part of a dog off of the screen, but not always. If you do not want the turtle to go off the screen, you can take steps to prevent it, e.g., use a lower value of screen_size_multiplier. Other strategies are possible as well.
- Number of times a program runs
- Number of objects that are drawn
- Anything, e.g., the random_dog program randomizes the numbers of pairs of legs for a dog
- Combination of objects to be put on the screen — you can create a list of objects, assign random numbers to each one and choose which one gets drawn at random, e.g., randomly select a number between 1 and 100 and use a decision tree to assign the range 1-30 to a dog, 31-40, to a moose, 41-70 to a house and 71-100 to a tree