简体   繁体   English

我如何使用 for 循环来弄清楚如何解决这个问题?

[英]How can I use for loops to figure out how to solve this?

enter image description here 5在此处输入图像描述5

I have to use a for loop to code this.我必须使用 for 循环来编写代码。 I have tried many times but it never works.我已经尝试了很多次,但它从来没有奏效。

enter code here
num_of_times = 5
for x in range(1, num_of_times, 5):
 for y in range(5, i+5):
  print(y, end= '')
 print('')

You can try this:你可以试试这个:

lst = []

for i in range(1, 5):
    lst.append(i * 5)
    print(*lst)

You basically want to print multiple of 5. That could be achieved with two nested for loops:您基本上想要打印 5 的倍数。这可以通过两个嵌套的 for 循环来实现:

max = 5
for i in range(max):
    line = ''
    for j in range(i+1):
        line += str(5 * (j+1)) + ' '
    print(line)

The first loop goes from 0 to max-1 and stores the index in i ;第一个循环从 0 到max-1并将索引存储在i中; the second loop goes from 0 to i+1 and stores the index in j .第二个循环从 0 到i+1并将索引存储在j中。 Then, the result of 5 * (j+1) is added to a variable called line printed at the end of the j-loop.然后,将5 * (j+1)的结果添加到在 j 循环末尾打印的名为line的变量中。 Feel free to follow the loops and the value of each variable at every step with a paper and a pen, that should help you.随意使用纸和笔在每一步中跟踪循环和每个变量的值,这应该对您有所帮助。

Here you go给你 go

for i in range(1,6):
    for j in range (1, i+1):
        print(j*5, end = ' ')
    print('')

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

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