Python Forum
Simplify a program using functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Simplify a program using functions (/thread-314.html)



Simplify a program using functions - nzieno - Oct-05-2016

Define the function [b]drawRing(x, y, radius, width, color)[/b], which draws a circle centered at (x,y) of the radiuswidth 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).

 



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


RE: Simplify a program using functions - Larz60+ - Oct-05-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+


RE: Simplify a program using functions - nzieno - Oct-05-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


RE: Simplify a program using functions - nilamo - Oct-05-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


RE: Simplify a program using functions - Ofnuts - Oct-05-2016

(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.


RE: Simplify a program using functions - nzieno - Oct-05-2016

I actually ended up getting it about 20 minutes after I posted this. But thats for getting back to me so promptly


RE: Simplify a program using functions - Kebap - Oct-05-2016

When you have a solution, you can show it here, and we can offer hints to improve it even more :)


RE: Simplify a program using functions - nzieno - Oct-06-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


RE: Simplify a program using functions - Ofnuts - Oct-06-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).