Posts: 10
Threads: 3
Joined: Oct 2016
Oct-05-2016, 02:52 PM
(This post was last modified: Oct-05-2016, 03:20 PM by metulburr.)
Define the function [b]drawRing(x, y, radius, width, color)[/b] , which draws a circle centered at (x,y) of the radius, width and color passed as arguments to this function. Create a new version of the OlympicSymbol.pyprogram on pages 23-24 of the textbook, in which you define, and use, this function to draw the rings in the Olympic symbol.
(Note: the width of the ring is used to set the size of the pen with turtle.pensize).
The purpose of this exercise is to show how functions can be used to reduce the amount of duplicated code in a program. This can potentially reduce the number of "points of failure," due to ,for example, copying-and-pasting practices, to just one (the definition of the function itself).
import turtle
turtle.color("blue")
turtle.penup()
turtle.goto(-110, -25)
turtle.pendown()
turtle.circle(45)
turtle.color("black")
turtle.penup()
turtle.goto(0, -25)
turtle.pendown()
turtle.circle(45)
turtle.color("red")
turtle.penup()
turtle.goto(110, -25)
turtle.pendown()
turtle.circle(45)
turtle.color("yellow")
turtle.penup()
turtle.goto(-55, -75)
turtle.pendown()
turtle.circle(45)
turtle.color("green")
turtle.penup()
turtle.goto(55, -75)
turtle.pendown()
turtle.circle(45)
turtle.done()
The program assigned is supposed to simply this code into a function but I am just not understanding at all how this is supposed to work.
Any help would be greatly appreciated
Posts: 12,039
Threads: 487
Joined: Sep 2016
This looks like an assignment.
basically, it's
def function_name(attributes):
context in your case, the only attribute you need is color.
the context is the part that doesn't change from call to call
then to call your function (you might want to call it change_color)
it would be:
function_name('blue') Larz60+
Posts: 10
Threads: 3
Joined: Oct 2016
import turtle
def DrawRing(x = -110, y = -25, color = "blue" , width = 2, radius = 45):
end = ''
color = "blue"
radius = 45
width = 2
x = -110
y = -25
turtle.color(color)
turtle.penup()
turtle.goto(x, y)
turtle.pensize(width)
turtle.pendown()
turtle.circle(radius)
turtle.done()
This is what I currently have this makes a blue circle
Posts: 3,458
Threads: 101
Joined: Sep 2016
First, I'd suggest moving the turtle.* functions so they're actually within the function. ...and then calling that function. A couple times, with different parameters. Then you're done :p
Posts: 687
Threads: 37
Joined: Sep 2016
Oct-05-2016, 05:07 PM
(This post was last modified: Oct-05-2016, 05:11 PM by Ofnuts.)
(Oct-05-2016, 03:12 PM)Larz60+ Wrote: def function_name(attributes):
context in your case, the only attribute you need is color.
and position...
(Oct-05-2016, 03:18 PM)nzieno Wrote: import turtle def DrawRing(x = -110, y = -25, color = "blue" , width = 2, radius = 45): end = '' color = "blue" radius = 45 width = 2 x = -110 y = -25 turtle.color(color) turtle.penup() turtle.goto(x, y) turtle.pensize(width) turtle.pendown() turtle.circle(radius) turtle.done() This is what I currently have this makes a blue circle
Not bad, but your function doesn't use its parameters, it replaces their value by fixed values, so calling the function will always draw the same circle.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Posts: 10
Threads: 3
Joined: Oct 2016
I actually ended up getting it about 20 minutes after I posted this. But thats for getting back to me so promptly
Posts: 470
Threads: 29
Joined: Sep 2016
Oct-05-2016, 11:59 PM
(This post was last modified: Oct-05-2016, 11:59 PM by Kebap.)
When you have a solution, you can show it here, and we can offer hints to improve it even more :)
Posts: 10
Threads: 3
Joined: Oct 2016
import turtle
def DrawRing(x = -110, y = -25, color = "blue" , width = 2, radius = 45):
turtle.color(color)
turtle.penup()
turtle.goto(x, y - radius)
turtle.pendown()
turtle.pensize(width)
turtle.circle(radius)
DrawRing()
DrawRing(0, -25, "black")
DrawRing(110, -25, "red")
DrawRing(-55, -75, "yellow")
DrawRing(55, -75, "green")
turtle.done()
here is my final code
Posts: 687
Threads: 37
Joined: Sep 2016
(Oct-06-2016, 03:57 PM)nzieno Wrote: import turtle
def DrawRing(x = -110, y = -25, color = "blue" , width = 2, radius = 45):
turtle.color(color)
turtle.penup()
turtle.goto(x, y - radius)
turtle.pendown()
turtle.pensize(width)
turtle.circle(radius)
DrawRing()
DrawRing(0, -25, "black")
DrawRing(110, -25, "red")
DrawRing(-55, -75, "yellow")
DrawRing(55, -75, "green")
turtle.done()
here is my final code
Not bad but as a matter of style, you shouldn't be using default arguments, for x, y, and color, and in any case the call for the blue circle should be made in full even if it uses the default values. Why? Because it allows your reader to see everything in the same place, and not have to hunt down the color of the 5th ring elsewhere in the code. A function makes it simpler for you the author, but it also makes simpler for the reader, who can remain at the necessary level of detail. I can read your code and guess that you are drawing rings of varoius colors, without having to read the DrawRing() definition (which, in many cases, could be in a different file anyway).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
|