Functions help

S

Scott W Dunning

Hello,

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?

Thanks for any help!

Scott
 
S

Steven D'Aprano

Hello,

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?


Sorry, I don't really understand your question. Could you show an example
of what you are doing?

Do you mean "add 5" or "*5"? "Add *5 doesn't really mean anything to me.
 
A

alex23

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?

The same way you repeat anything in Python: with a loop construct.

for _ in range(5):
func()
 
M

Mark Lawrence

The same way you repeat anything in Python: with a loop construct.

for _ in range(5):
func()

For the benefit of newbies, besides the obvious indentation error above,
the underscore basically acts as a dummy variable. I'll let the
language lawyers give a very detailed, precise description :)
 
S

Scott W Dunning

Sorry, I don't really understand your question. Could you show an example
of what you are doing?

Do you mean "add 5" or "*5"? "Add *5 doesn't really mean anything to me.
Sorry I forgot to add the code that I had to give an example of what I was talking about. I’ll put it below, sorry that it’s so long. A couple of people have basically answered my question though. I take it was I was talking about was a loop, which I haven’t learned in school yet but, it seems semi self-explanatory. As you can see I added a loop in there about half way down the code (i put it in bold) and it seemed to do what I want. Now I’m going to try and do what Rhodri suggested, a range function? I’m not sure exactly what that’ll do but I think it’ll clean up my code more and make things easier to call?

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)

for i in range (5):
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)
 
T

Travis Griggs

For the benefit of newbies, besides the obvious indentation error above, the underscore basically acts as a dummy variable. I'll let the language lawyers give a very detailed, precise description :)

You mean a dummy name binding, right? If we say "variable" we might confuse those newly arrived pilgrims from other language kingdom.



(If you squint hard, I think there's some <facetious> tags in there :) )
 
S

Scott W Dunning

I understood what you meant because I looked up loops in the python documentation since we haven’t got there yet in school.
 
M

Mark Lawrence

Your message came through fine for me (viewing as mailing list in
gmail). Mark's client must be dropping spaces.

I'm reading gmane.comp.python.general using Thunderbird 24.3.0 on Windows 7.
 
M

MRAB

I'm reading gmane.comp.python.general using Thunderbird 24.3.0 on Windows 7.
It looked OK to me (also using Thunderbird).

However, examining the source, I can see that the first line is
indented with 5 spaces and the second line with 1 tab.
 
R

rurpy

I'm reading gmane.comp.python.general using Thunderbird 24.3.0 on Windows 7.

The original message was properly indented on Google Groups.
Perhaps you should switch to GG or some non-broken client that
doesn't mangle whitespace.
 
S

sffjunkie

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?

The following answers your question but is probably not a method you want to employ.

from functools import partial

def a(ch):
print(ch)

def b(x):
print(x + 10)

[x() for x in [partial(a, 'd'), partial(b, 10)]*5]

Produces

d
14
d
14
d
14
d
14
d
14

--Simon
 
S

sffjunkie

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?

The following answers your question but is probably not a method you want to employ.

from functools import partial

def a(ch):
print(ch)

def b(x):
print(x + 4)

[x() for x in [partial(a, 'd'), partial(b, 10)]*5]

Produces

d
14
d
14
d
14
d
14
d
14

--Simon
 
M

Mark Lawrence

The original message was properly indented on Google Groups.
Perhaps you should switch to GG or some non-broken client that
doesn't mangle whitespace.

MRAB has confirmed that as always Thunderbird is working perfectly,
thank you. But you can stick with your bug ridden, badly flawed tool as
long as you like, just expect me to keep complaining until google fixes
it, or people stop using it, or people follow the instructions that were
put up on a Python web site to prevent the bugs showing up.
 
E

Ethan Furman

The original message was properly indented on Google Groups.
Perhaps you should switch to GG or some non-broken client that
doesn't mangle whitespace.

LOL! How long have you waited to say that? ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top