Let's say I have two functions.
One has two arguments, but one of them is a keyword arg, so it doesn't come up often.
def foo(x, _y=1):
print str(x)
The other has one argument.
def bar(x)
Ideally, It would run foo
x
times, each time with foo
's x
argument being one larger (starting at one), until it is ran with bar
's x
argument. Due to the varying number of times it should be run, as well as the fact this number may potentially be in the thousands, it wouldn't be practical to make a slightly-varying line of code for every number between 1
and x
.
In other words, how would I make a function run multiple times, once for each number in range(1, x + 1)
?
Never mind. As Ignacio Vazquez-Abrams helpfully points out, this could be done with a simple for
loop.
After all, with a simple little snippet such as:
def bar(x):
for val in range(1, x + 1):
foo(val)
... we get our goal, for example:
bar(5)
results in
foo(1)
foo(2)
foo(3)
foo(4)
foo(5)
(Thanks to tdelany for giving this clumsy novice a tip.)
...I really need to brush up on Python.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.