擅长:python、mysql、java
<p>将您的功能放入列表中:</p>
<pre><code>functions = [function_one, function_two, function_three, ...]
</code></pre>
<p>在它们上面循环:</p>
<pre><code>n = int(input('Number: '))
for i in range(n):
functions[i]()
</code></pre>
<p>现在你的要求是:</p>
<blockquote>
<p>If user gives 11 , after calling the 10 functions , it should start calling the rest 1 from the Beginning</p>
</blockquote>
<p>有很多方法可以做到这一点,但是如果我们想坚持上面的代码,我们可以使用模运算符(<code>%</code>)来允许“越过”结尾:</p>
<pre><code>n = int(input('Number: '))
for i in range(n):
functions[i % len(functions)]()
</code></pre>
<p>您还可以完全更改方法,使用<a href="https://docs.python.org/2/library/itertools.html#itertools.cycle" rel="noreferrer">^{<cd2>}</a>而不是循环遍历列表的索引,并使用内置的<a href="https://docs.python.org/3/library/functions.html#next" rel="noreferrer">^{<cd3>}</a>来获取函数:</p>
<pre><code>import itertools
functions = [function_one, function_two, function_three, ...]
functions_it = itertools.cycle(functions)
n = int(input('Number: '))
for i in range(n):
next(functions_it)()
</code></pre>