Re: [Tutor] Function help
Scott W Dunning wrote: > > On Feb 23, 2014, at 2:26 AM, Peter Otten <__pete...@web.de> wrote: >> If you want to make rows with more or less stars, or stars in other >> colors you could add parameters: >> >> def star_row(numstars, starcolor): >>for i in range(numstars): >>fillstar(starcolor) >>space(25) >> >> Your code will then become >> >> star_row(6, red) >> row(25) >> star_row(5, red) >> row(25) >> > I have a question with the above loop function. Why couldn’t row(25) be > added into the function so that wouldn’t have to placed in between every > star_row()? That's of course possible. >> which still shows a repetetive pattern and thus you can simplify it with >> another loop. You should be able to find a way to write that loop with >> two star_row() calls on a single iteration, but can you do it with a >> single call too? > Not sure I understand what you mean in the above paragraph? What you found out later and put in you next post: This repetetive pattern > star_row(5) # oops, typo > row(25) > star_row(5) > row(25) > star_row(6) > row(25) > star_row(5) > row(25) > star_row(6) > row(25) > star_row(5) > row(25) > star_row(6) > row(25) > star_row(5) > row(25) > star_row(6) can be turned into this for loop: > This is what I’m thinking… > > for I in range(4) > star_row(5) > row(25) > star_row(6) > row(25) > > Am I at all close? You hit the jackpot :) Now on to the next challenge: for row_index in range(9): row_width = ... star_row(row_width) row(25) Can you replace the ... with a calculation to get row_width=6 for even and row_width=5 for odd rows? Hint: look at the % ("modulo") operator. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
On 24/02/14 02:04, Scott W Dunning wrote: *Also, does anyone know anything about turtle where I can try and move the starting point to the upper left hand corner? * dir() and help() are your friends. After doing dir(turtle) I spooted this likely looking candidate and ran help on it: -- setposition(x, y=None) Move turtle to an absolute position. Aliases: setpos | setposition | goto: Arguments: x -- a number or a pair/vector of numbers y -- a number None call: goto(x, y) # two coordinates --or: goto((x, y)) # a pair (tuple) of coordinates --or: goto(vec) # e.g. as returned by pos() Move turtle to an absolute position. If the pen is down, a line will be drawn. The turtle's orientation does not change. etc -- Similarly, I found the window_width() and window_height() methods. So, to get to the top left corner you'd goto( -window_width/2, window_height/2 ) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
On Feb 23, 2014, at 2:26 AM, Peter Otten <__pete...@web.de> wrote: > which still shows a repetetive pattern and thus you can simplify it with > another loop. You should be able to find a way to write that loop with two > star_row() calls on a single iteration, but can you do it with a single call > too? So, I was able to cut it down a bit but I’m having a hard time trying to cut it down with another loop and a single call. Wouldn’t what you’re saying in the above paragraph have to be another function with a loop inside? Or are you saying just another loop will suffice? Any hints? Here is what I got so far, I’ll put the loop in question in bold. from turtle import * from math import sin, sqrt, radians def star(width): R = (width)/(2*sin(radians(72))) A = (2*width)/(3+sqrt(5)) penup() left(18) penup() forward(R) pendown() left(162) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) penup() left(162) forward(R) left(162) showturtle() def fillstar(color): fillcolor(color) begin_fill() star(25) end_fill() red = "red" fillstar(red) def space(width): penup() forward(2*width) pendown() space(25) def row(width): penup() right(90) forward(width) right(90) forward(11*width) right(180) pendown() def star_row(numberstars): for i in range (numberstars): fillstar(red) space(25) star_row(5) row(25) star_row(5) row(25) star_row(6) row(25) star_row(5) row(25) star_row(6) row(25) star_row(5) row(25) star_row(6) row(25) star_row(5) row(25) star_row(6) This is what I’m thinking… for I in range(4) star_row(5) row(25) star_row(6) row(25) Am I at all close? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
On Feb 23, 2014, at 2:26 AM, Peter Otten <__pete...@web.de> wrote: > If you want to make rows with more or less stars, or stars in other colors > you could add parameters: > > def star_row(numstars, starcolor): >for i in range(numstars): >fillstar(starcolor) >space(25) > > Your code will then become > > star_row(6, red) > row(25) > star_row(5, red) > row(25) > I have a question with the above loop function. Why couldn’t row(25) be added into the function so that wouldn’t have to placed in between every star_row()? > > which still shows a repetetive pattern and thus you can simplify it with > another loop. You should be able to find a way to write that loop with two > star_row() calls on a single iteration, but can you do it with a single call > too? Not sure I understand what you mean in the above paragraph? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
On Feb 23, 2014, at 2:26 AM, Peter Otten <__pete...@web.de> wrote > a programmer would think "for loop” immediately That’s what I thought. It just seemed like way to much to keep repeating everything over and over. I knew there had to be a better way we just haven’t learned loops in school yet. > > for i in range(5): >fillstar(red) >space(25) Awesome, I was able to cut everything down quite a bit but, now like you say below I’m still repeating the loop. I’m gonna see if I can make a function with the loop to cut it down even further. Here is what I was able to cut it down to so far with your help. I’ll paste the new code when I make a function with the loop and maybe you guys can help me see if it look any better/easier. Also, does anyone know anything about turtle where I can try and move the starting point to the upper left hand corner? Thanks again! Scott from turtle import * from math import sin, sqrt, radians def star(width): R = (width)/(2*sin(radians(72))) A = (2*width)/(3+sqrt(5)) penup() left(18) penup() forward(R) pendown() left(162) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) penup() left(162) forward(R) left(162) showturtle() def fillstar(color): fillcolor(color) begin_fill() star(25) end_fill() red = "red" fillstar(red) def space(width): penup() forward(2*width) pendown() space(25) for i in range (5): fillstar(red) space(25) def row(width): penup() right(90) forward(width) right(90) forward(11*width) right(180) pendown() row(25) for i in range (5): fillstar(red) space(25) row(25) for i in range (6): fillstar(red) space(25) row(25) for i in range (5): fillstar(red) space(25) row(25) for i in range (6): fillstar(red) space(25) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
On Feb 23, 2014, at 5:31 AM, Dave Angel wrote: > > Welcome to the tutor forum also, Scott. You'll find it works very > similarly to python-list, and has many of the same people on it. > I'm not sure how you tried to attach source, but please be aware > that this is a text list - anything other than plain text will > probably be invisible or inconvenient to someone. Just paste > snippets inline when needed. I actually forgot to paste the code before I sent the email. > > What you're looking for is a loop. for and while are the two > keywords for looping. In this case, since you know how many > times you want to go round, loop is appropriate. Build a > collection or iterator of length 5, and loop over it. range is > designed for the purpose: > > for index in range (5): > dosomething > moresomething (index) Thank you for this help. I believe I understand. At least enough to do a simple loop for what I need. I’ll check back and paste my code after and maybe you can tell me if there is anything I could be doing better/easier. Thanks again!! Scott ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
Scott W Dunning Wrote in message: > I am VERY new to python (programming too). I had a question regarding > functions. Is there a way to call a function multiple times without > recalling it over and over. Meaning is there a way I can call a function and > then add *5 or something like that? I am trying to code an American Flag > using turtle for class so Iâll post the code I have so far below. As you > can see towards the bottom I recall the functions to draw the stars, fill in > color and give it spacing. I was wondering if there was a way to cut down on > all that some how? > Welcome to the tutor forum also, Scott. You'll find it works very similarly to python-list, and has many of the same people on it. I'm not sure how you tried to attach source, but please be aware that this is a text list - anything other than plain text will probably be invisible or inconvenient to someone. Just paste snippets inline when needed. What you're looking for is a loop. for and while are the two keywords for looping. In this case, since you know how many times you want to go round, loop is appropriate. Build a collection or iterator of length 5, and loop over it. range is designed for the purpose: for index in range (5): dosomething moresomething (index) Everything that's indented will happen 5 times. Later you'll want to learn continue and break, which can alter the simple flow. And presumably you already know if. import, for, while, if, elif, else, break, continue Incidentally, your star function could have been much shorter with a loop. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
Scott W Dunning writes: > I am VERY new to python (programming too). Welcome! You should establish the fundamentals of Python, by working through the Python tutorial http://docs.python.org/3/tutorial/>. Begin at the beginning, and execute all the examples, and experiment with them to satisfy yourself that you understand what is being demonstrated; then, move on to the next. Continue until you have a good grasp of all the basics of Python. > I had a question regarding functions. Is there a way to call a > function multiple times without recalling it over and over. When you have worked through the tutorial, you will understand how loops can address problems like this. Enjoy learning the basics of programming! -- \ “As scarce as truth is, the supply has always been in excess of | `\ the demand.” —Josh Billings | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
Scott W Dunning wrote: > > On Feb 23, 2014, at 1:12 AM, Scott W Dunning wrote: > >> I am VERY new to python (programming too). I had a question regarding >> functions. Is there a way to call a function multiple times without >> recalling it over and over. Meaning is there a way I can call a function >> and then add *5 or something like that? I am trying to code an American >> Flag using turtle for class so I’ll post the code I have so far below. >> As you can see towards the bottom I recall the functions to draw the >> stars, fill in color and give it spacing. I was wondering if there was a >> way to cut down on all that some how? >> >> Thanks for any help! The example helps a lot! And of course this process of "cutting down", achieving complex tasks by doing simple things repetetively is what programmers do all the time. There is even a principle called "DRY" (don't repeat yourself) meaning that if you have to write something twice in your code you are doing it wrong. Looking at > fillstar(red) > space(25) > fillstar(red) > space(25) > fillstar(red) > space(25) > fillstar(red) > space(25) > fillstar(red) > space(25) a programmer would think "for loop" immediately for i in range(5): fillstar(red) space(25) and as this sequence occurs more than once in your code you should make it a function. For example: def star_row(): for i in range(5): fillstar(red) space(25) If you want to make rows with more or less stars, or stars in other colors you could add parameters: def star_row(numstars, starcolor): for i in range(numstars): fillstar(starcolor) space(25) Your code will then become star_row(6, red) row(25) star_row(5, red) row(25) ... which still shows a repetetive pattern and thus you can simplify it with another loop. You should be able to find a way to write that loop with two star_row() calls on a single iteration, but can you do it with a single call too? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function help
On Feb 23, 2014, at 1:12 AM, Scott W Dunning wrote: > I am VERY new to python (programming too). I had a question regarding > functions. Is there a way to call a function multiple times without > recalling it over and over. Meaning is there a way I can call a function and > then add *5 or something like that? I am trying to code an American Flag > using turtle for class so I’ll post the code I have so far below. As you can > see towards the bottom I recall the functions to draw the stars, fill in > color and give it spacing. I was wondering if there was a way to cut down on > all that some how? > > Thanks for any help! > > Scott from turtle import * from math import sin, sqrt, radians def star(width): R = (width)/(2*sin(radians(72))) A = (2*width)/(3+sqrt(5)) penup() left(18) penup() forward(R) pendown() left(162) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) left(144) forward(A) right(72) forward(A) penup() left(162) forward(R) left(162) showturtle() def fillstar(color): fillcolor(color) begin_fill() star(25) end_fill() red = "red" fillstar(red) def space(width): penup() forward(2*width) pendown() space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) def row(width): penup() right(90) forward(width) right(90) forward(11*width) right(180) pendown() row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) row(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) fillstar(red) space(25) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Function help
I am VERY new to python (programming too). I had a question regarding functions. Is there a way to call a function multiple times without recalling it over and over. Meaning is there a way I can call a function and then add *5 or something like that? I am trying to code an American Flag using turtle for class so I’ll post the code I have so far below. As you can see towards the bottom I recall the functions to draw the stars, fill in color and give it spacing. I was wondering if there was a way to cut down on all that some how? Thanks for any help! Scott ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] function help
Ashley F wrote: ok...here's the function I've written so far. def padWithGaps(seq): for letter in seq: letter="-" line=len(seq) dashline=line*letter return dashline I don't think that's a useful function. It seems to do a HUGE amount of work that just keeps getting thrown away. Let's say seq = "GACT", this function will do the following: letter = "G" letter = "-" line = 4 # len of seq dashline = "" letter = "A" letter = "-" line = 4 dashline = "" letter = "C" letter = "-" line = 4 dashline = "" letter = "T" letter = "-" line = 4 dashline = "" It does everything four times. Now imagine that seq is a million characters long instead of four! This will do the job *much* faster: def padWithGaps(seq): return "-" * len(seq) *Much* faster, much easier to read. [...] I don't know what to do at the end of my loop but what I'm trying to do in pseudocode is: "if that alignment has the best score seen so far save the score and the alignment print the best score and the best alignment" You need to store the current best score and the current best alignment. Then each time around the innermost loop, you need to calculate the score and alignment (as you already do), and compare them to the best seen so far. If they are worse or equal, you don't need to do anything, just go on to the next loop as normal. But if they are better, then you need to update the best score and alignment, and print them. if score > best_score: best_score = score best_alignment = alignment print 'best seen so far is', score, alignment Does that help? Try writing the code, and if you still can't get it working, ask again. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] function help
I'm not sure of the answer to your question, because i'm not sure I understand it. Perhaps you could rephrase it? But, there is some other stuff I noticed though: def padWithGaps(seq): for letter in seq: letter="-" line=len(seq) dashline=line*letter return dashline This function is fairly inefficient. Let's say you pass the function a sequence three items long something like a_list = ['a','b','c']. Your telling Python to create a dashed line three dashes long three separate times. So, you do it for the 'a' part of the sequence, the 'b' and the 'c'. You can condense this to something like: def padWithGaps(seq): lent = len(seq) return lent*'-' And you really don't need to have a separate function for that, as you can just do it on the fly Another thing, whenever your taking a slice of a list and the first argument is 0, you can just omit that. For example, from my list a_list example above a_list[0:2] is equivalent to a_list[:2] As for the rest of them, what you could do is pass each function data that you would expect to be passed under normal operation (assuming this is possible of course) and instead of using return use print instead and see if the results are what you expect. What I mean is, let's say I pass the results from the padWithGaps (the results of which I call dashline to be consistent) to the next function: def replace(someString,position,letter): first=someString[0:position] second=letter third=someString[position+1:len(someString)] newString=first+second+third return newString So, I can do something like z = replace(dashline,0,'a') and then print z. What the results should be is "a--". def findScore(first,second): count=0 for position in range(0,len(first)): if first[position]==second[position]: count=count+1 position=position+1 return count With this function here, there is a few things you can do to optimize and clean up clutter as well. I only point this out because it pays huge dividends in the long run (or it has for me at least). The first thing though is that you don't need to have the line position=position+1. Python will go to the next item in the list "position" whether you tell it to or not. So below is the same thing but without the position +=1 line: def findScore(first,second): count=0 for position in range(0,len(first)): if first[position]==second[position]: count=count+1 return count The other thing I would suggest is to use enumerate. Enumerate is your friend. And the last thing I would suggest is whenever you want to do something like a = a+1, you can just do a +=1. > def findScore2(first,second): count=0 for i, position in enumerate(first): if position==second[i]: count+=1 return count enumerate returns both the next item in the list and the position of that item in the list (in this case, I called that variable i). So you will find that if you run findScore2 you will have the same results as findScore. Or at least you should ;) On Mon, Feb 7, 2011 at 11:45 AM, Ashley F wrote: > ok...here's the function I've written so far. > > def padWithGaps(seq): > for letter in seq: >letter="-" >line=len(seq) >dashline=line*letter > return dashline > > def replace(someString,position,letter): > first=someString[0:position] > second=letter > third=someString[position+1:len(someString)] > newString=first+second+third > return newString > > ##findScore("MPFVS","MS-V-") would return a score of 2 > def findScore(first,second): > count=0 > for position in range(0,len(first)): > if first[position]==second[position]: > count=count+1 > position=position+1 > return count > > #shorter is a 3 amino acid sequence > ##longer is any length sequence greater than 3 > ###i=first amino acid; j=second amino acid; k=third amino acid > def findAlignment(shorter,longer): > for i in range(0,len(longer)-2): > for j in range(1,len(longer)-1): > for k in range(2,len(longer)): >dashline=padWithGaps(longer) >nextLine=replace(dashline,i,shorter[0]) >nextNext=replace(nextLine,j,shorter[1]) >alignment=replace(nextNext,k,shorter[2]) >score=findScore(longer,alignment) >don't know what to do here > print longer > print alignment > print "Score = " + str(score) > > I don't know what to do at the end of my loop but what I'm trying to do in > pseudocode is: > "if that alignment has the best score seen so far > save the score and the alignment > print the best score and the best alignment" > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > ht
[Tutor] function help
ok...here's the function I've written so far. def padWithGaps(seq): for letter in seq: letter="-" line=len(seq) dashline=line*letter return dashline def replace(someString,position,letter): first=someString[0:position] second=letter third=someString[position+1:len(someString)] newString=first+second+third return newString ##findScore("MPFVS","MS-V-") would return a score of 2 def findScore(first,second): count=0 for position in range(0,len(first)): if first[position]==second[position]: count=count+1 position=position+1 return count #shorter is a 3 amino acid sequence ##longer is any length sequence greater than 3 ###i=first amino acid; j=second amino acid; k=third amino acid def findAlignment(shorter,longer): for i in range(0,len(longer)-2): for j in range(1,len(longer)-1): for k in range(2,len(longer)): dashline=padWithGaps(longer) nextLine=replace(dashline,i,shorter[0]) nextNext=replace(nextLine,j,shorter[1]) alignment=replace(nextNext,k,shorter[2]) score=findScore(longer,alignment) don't know what to do here print longer print alignment print "Score = " + str(score) I don't know what to do at the end of my loop but what I'm trying to do in pseudocode is: "if that alignment has the best score seen so far save the score and the alignment print the best score and the best alignment" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor