简体   繁体   English

制作嵌套的for循环(Python 3.x)

[英]Making a nested for loop (Python 3.x)

So what I am trying to do is make this "Triangle" in python with for loops: 所以我想做的是用for循环在python中创建这个“ Triangle”:

帕斯卡

But with the text like so: 但是这样的文字:

0|0
1|01
2|012
3|0123
4|01234

What the output that I currently have is: 我目前的输出是:

0|01234
1|01234
2|01234
3|01234
4|01234

And here is the code for the output: 这是输出的代码:

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(n):
            answer = answer + str(col)
        print(answer)

pascal(5)

My question is, how the heck do I make the ouput do this?(Im stumped as to what im supposed to do): 我的问题是,我该怎么做才能做到这一点?(我为自己应该做的事情而烦恼):

0|0
1|01
2|012
3|0123
4|01234



If anyone would like to see what the hell I was trying to accomplish, heres my solution 如果有人想看看我到底想完成什么,这就是我的解决方案

Soooooooo, this blue triangle: 太好了,这个蓝色三角形:

帕斯卡

turns into the pascal triangle, by "n choose k": 变成帕斯卡三角形,按“ n选择k”:

式

I was trying to figure out the for loops so I can have the basic setup done(which is the blue triangle), which you guys helped with :) 我试图找出for循环,以便完成基本设置(蓝色三角形),你们帮助了:)

So the code that I came up with to get the n choose k is this: 因此,我想出的让n选择k的代码是这样的:

def factorial(n):
    answer = 1
    for number in range(2, n+1):
       answer = answer * number
    return answer


def pascal(n):
    answer = ""
    for rows in range(n):
        answer = ""
        for col in range(rows+1):
            answer = answer + str( int(factorial(rows) / (factorial(col)*factorial(rows-(col)))) )

        print(answer)
pascal(10)

The factorial() is the exclamation point in the n choose k formula and I made the rest of the formula with this code: factorial()是n选择k公式中的感叹号,我使用以下代码制作了其余公式:

factorial(rows) / (factorial(col)*factorial(rows-(col))) 

So any n that is greater than 0, makes a pascal triangle :) 因此,任何大于0的n都会形成一个帕斯卡三角形:)

You are close. 你近了 When printing each row you don't want to go all the way to n . 当打印每一行时,您并不想一直到n The inner loop should stop at rows , so change for col in range(n) to for col in range(rows+1) . 内部循环应rows停止,因此将for col in range(n) for col in range(rows+1)更改for col in range(n) for col in range(rows+1)

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(rows+1):
            answer = answer + str(col)
        print(answer)

pascal(5)

Just modify the second for 只是修改第二for

Code: 码:

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(rows + 1): # Modify this
            answer = answer + str(col)
        print(answer)

pascal(5)

Why? 为什么? - Because your first for is for the number of lines, the second for is for the elements in that line, you don't want to loop over, let's say, 5 again, just over the correct number of elements, in this case, it will be the number of rows. -因为您的第一个for代表行数,第二个for代表行中的元素,所以您不想再遍历5 ,恰好是正确的元素数,在这种情况下,这将是行数。

Your code was correct but your inner loop shouldn't loop to n . 您的代码是正确的,但您的内部循环不应循环到n You should replace it with rows+1 . 您应该将其替换为rows+1

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(rows+1):
            answer = answer + str(col)
        print(answer)

pascal(5)

output: 输出:

0|0
1|01
2|012
3|0123
4|01234

Honestly, I wouldn't use a for loop to do this. 老实说,我不会使用for循环来做到这一点。 A while loop will be much more efficient for what you want to do. while循环对于您要执行的操作将更加有效。 For instance: 例如:

pascal(n):
  answer=''
  answerright = ''
  i=0
  while i <= n:
    answerright += str(i)
    answer += str(i) + '|' + answerright + '\n'
    i+=1
  return answer

That will generate what you're looking for, without overly complicating the code. 这将生成您要查找的内容,而不会使代码过于复杂。 You don't say if you are required to use for loops, but if not, this is the better solution. 您没有说是否需要使用for循环,但是如果不是,这是更好的解决方案。 In python, all for loops are really foreach loops, always remember that. 在python中,所有for循环实际上都是foreach循环,请记住这一点。

This should get you on the right path: 这应该使您走上正确的道路:

for row in range (5):
  row_string = str(row) + "|"

  for i in range(row+1):
    row_string += str(i)

  print row_string

You only want to iterate up to the current row (+1 since we're zero-indexed here) in your inner for-loop. 您只想在内部for循环中迭代到当前行(+1,因为我们在这里为零索引)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM