具有不同数量的For循环(python) [英] Function with varying number of For Loops (python)

查看:173
本文介绍了具有不同数量的For循环(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很难解释。

我想创建一个包含嵌套for循环的函数,其中
的数量是成比例的传递给函数的参数。

这是一个假设的例子:

 函数(2)

...涉及... $ b $ (y):b

 对于范围(y)中的x:
对于范围(y)中的x:
do_whatever()

另一个例子...

 函数(6)

...涉及... $ b $ (y):b

 对于范围(y)中的x:
对于范围(y)中的x:
对于范围(y) (y)的b $ b:x在范围(y)中的

在范围(y)中的x:
whatever()

for循环(y)的变量实际上并不在嵌套代码中使用。

你的第一个想法可能是创建一个循环,范围是数字参数的权力... ...
这可以不工作,因为产品会很大。我有必要的情况下,有8个嵌套for循环。

在for循环范围内的产品太大。



还有其他参数需要传递给函数,但我可以自己处理。

这里是代码
(它创建了Snowflake Fractal)

 来自turtle import * 
长度= 800
速度(0)

def迷你(长度):
(3):
转发(长度)
向右(60)
$ b $ penup()
setpos(-500, 0)
pendown()

选择=输入(输入复杂度)

如果选择== 1:
3):
前进(长度)
剩下(120)

elif choice == 2:
(x)范围(3):
Mini (长度/ 3)
剩下(120)

如果选择== 3:
(6)范围内的x:
Mini(长度/ 9)
右(60)
迷你(长/ 9)
(120)

如果选择== 4:
在范围内(6):
在范围内(2):
迷你(长度/ 27)
向右(60)
迷你(长度/ 27)
剩下(120)
正确(180)
在范围内(2):
迷你(长度/ 27)
正确60)
如果选择的话,可以选择Mini(长度/ 27)
(120)

5:
(6):
(2)中的z:
(2):
(2):
Mini
Mini(长度/ 81)
剩下(120)
正确(180)
(2):
Mini(长度/ 81)
右(60)
右(180)
右(180)

== 6:
fo范围内的rc(6):b范围内的
(2):范围内的
(2):范围(2)中的

范围(2):
(2):
迷你(长度/ 243)
向右(60)
迷你(长度/ 243)
(120)
右(180)
(2):
迷你(长度/ 243)
正确(60)
迷你(长度/ 243 )
(120)
权利(180)
权利(180)
权利(180)
权利(180)

(6):
在范围(2)中为

在范围(2)中为c:
在范围内(d) 2):
为e范围(2):
范围(2)中的f:
范围(2)中的y:
范围(2)中的x:b $ b Mini(长度/ 729)
右(60)
最小(长度/ 729)
(120)
正确(180)
(2) $ b $ Mini(长度/ 729)
正确(60)
小型(长度/ 729)
剩下(120)
正确(180)
正确(180)
right(180)
right(180)
right(180)
right(180)

我很感激你可以给我任何帮助l,

,但是如果你建议一个不同的方法(比如递归),
请不要粘贴代码;相反,建议一些想法,可以把我放在正确的方向。
$ b

(该算法是为一个专家数学作业)




规格:

Python 2.7.1

Turtle

IDLE

Windows7

解决方案

这个问题可以通过递归来解决。我只是在这里写一个算法,因为我相信这可能是一个普遍的问题。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ r $数字 - 1)
else
在范围内的x(y)
whatever()


My problem is difficult to explain.

I want to create a function that contains nested for loops,
the amount of which is proportional to an argument passed to the function.

Here's a hypothetical example:

Function(2)

...would involve...

for x in range (y):
    for x in range (y):
        do_whatever()

Another example...

  Function(6)

...would involve...

for x in range (y):
    for x in range (y):
        for x in range (y):
            for x in range (y):
                for x in range (y):
                    for x in range (y):
                        whatever()

The variables of the for loop (y) are NOT actually used in the nested code.

Your first thought might be to create ONE for loop, with a range that is to the power of the number argument...
THIS CAN NOT WORK because the product would be HUGE. I have instances required where there are 8 nested for loops.
The product is too large for a range in a for loop.

There are other arguments needed to be passed to the function, but I can handle that myself.

Here's the code (it creates the Snowflake Fractal)

from turtle import *
length = 800
speed(0)

def Mini(length):
    for x in range (3):
        forward(length)
        right(60)

penup()
setpos(-500, 0)
pendown()   

choice = input("Enter Complexity:")

if choice == 1:
    for x in range (3):
        forward(length)
        left(120)

elif choice == 2:
    for x in range (3):
        Mini(length/3)
        left(120)

if choice == 3:
    for x in range (6):
        Mini(length/9)
        right(60)
        Mini(length/9)
        left(120)

if choice == 4:
    for y in range (6):
        for x in range (2):
            Mini(length/27)
            right(60)
            Mini(length/27)
            left(120)
        right(180)
        for x in range (2):
            Mini(length/27)
            right(60)
            Mini(length/27)
            left(120)

if choice == 5:
    for a in range (6):
        for z in range (2):
            for y in range (2):
                for x in range (2):
                    Mini(length/81)
                    right(60)
                    Mini(length/81)
                    left(120)
                right(180)
                for x in range (2):
                    Mini(length/81)
                    right(60)
                    Mini(length/81)
                    left(120)
            right(180)
        right(180)

if choice == 6:
    for c in range (6):
        for b in range (2):
            for a in range (2):
                for z in range (2):
                    for y in range (2):
                        for x in range (2):
                            Mini(length/243)
                            right(60)
                            Mini(length/243)
                            left(120)
                        right(180)
                        for x in range (2):
                            Mini(length/243)
                            right(60)
                            Mini(length/243)
                            left(120)
                    right(180)
                right(180)
            right(180)
        right(180)

if choice == 7:
    for a in range (6):
        for b in range(2):
            for c in range (2):
                for d in range (2):
                    for e in range (2):
                        for f in range (2):
                            for y in range (2):
                                for x in range (2):
                                    Mini(length/729)
                                    right(60)
                                    Mini(length/729)
                                    left(120)
                                right(180)
                                for x in range (2):
                                    Mini(length/729)
                                    right(60)
                                    Mini(length/729)
                                    left(120)
                            right(180)
                        right(180)
                    right(180)
                right(180)
            right(180)
        right(180)

I'd appreciate any help you can give me at all,
though if you suggest a different method (such as recursion),
please don't just paste the code; instead, suggests some ideas that could put me in the right direction.

(The algorithm is for a Specialist Math Assignment)


specs:
Python 2.7.1
Turtle
IDLE
Windows7

解决方案

This problem can be solved by recursion. I am just writing an algorithm here, since I believe this can be a general problem.

function Recurse (y, number) 
   if (number > 1)
      Recurse ( y, number - 1 )
   else
      for x in range (y)
      whatever()

这篇关于具有不同数量的For循环(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆