Python Beginner Make a Number Pyramid with Double Loop Question

Asked 2 years ago, Updated 2 years ago, 11 views

for x in range(0, 7) :
    y = 7 - x
    print(' ' * y + '*'*x)

If you squeeze the cord like this,

Here's the result.

What I want to do is

This is it. I'm going to use * to make a salty code.

for i in range(0, 7) :
    for j in range(0,  i):
        x = 7-i
        print(' '* x +str(j), end='')
    print()

I made the chords like this. But now that I do this, the execution result is

It comes out like this. What should I do?

python

2022-09-20 21:59

3 Answers

length = 7
for i in range(length):
    print(f"{''.join(map(str, range(i)))}".rjust(length))


      0
     01
    012
   0123
  01234
 012345


2022-09-20 21:59

What the questioner intended was probably the following code.

for i in range(0, 7) :
    x = 7-i
    print(' '*x, end='')
    for j in range(0,  i):        
        print(str(j), end='')
    print()

It may be difficult because I'm a beginner, but I think I could have done it if I put a little effort into it. Think for yourself.


2022-09-20 21:59

Try it with scalar If you're comfortable with Python, try scalar.

val l = 7
(0 to l - 1).foreach { i: Int => 
  println(s"%${l}s".format(s"${(0 to i).collect {case i => i.toString}.mkString("")}")) 
}
      0
     01
    012
   0123
  01234
 012345
0123456


2022-09-20 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.