Python Forum
Simplify a program using functions
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simplify a program using functions
#1
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
Reply
#2
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+
Reply
#3
  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
Reply
#4
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
Reply
#5
(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
Reply
#6
I actually ended up getting it about 20 minutes after I posted this. But thats for getting back to me so promptly
Reply
#7
When you have a solution, you can show it here, and we can offer hints to improve it even more :)
Reply
#8
   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
Reply
#9
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good Morning help simplify raymond2688 3 2,402 Jul-22-2019, 08:27 PM
Last Post: jefsummers
  Functions and program question Zei 7 4,936 Aug-31-2017, 01:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020