Python numeric cascading output questions

Asked 2 years ago, Updated 2 years ago, 14 views

Hello, I've been trying to solve Python problems and I have a question, so I'm posting it.

The goal is to take the natural number N and print it out from 1 to N in the first row, 2 in the second row, and 3 in the third row.

x = int(input())

n=1



for i in range(1, x+1):

    sum = (n+1)*n // 2

    print(i , end = ' ')

    if(sum == i):

        print()

        n+=1

When running

1

23

456

78910

It's coming out well. sum = (n+1)*n//2 This part was solved with the help of a friend.

So the question is, is there a way to solve it using multiple for statements instead of making a formula like that?

python

2022-09-20 19:37

2 Answers

So if you receive n, the total n lines will be output, right?
And k always has k natural numbers on the line.

Then there are two things to repeat.

1. 1 and n to water. (한 줄씩 출력하기 위해서)
Line is now 2. k the first line said, 1 and k to turn . (숫자를 하나씩 출력하기 위해서)

And it would be easier if you save the last number you took in a separate variable p and update it.

Now! Let's make your friend's nose flat.


2022-09-20 19:37

Please refer to the code below.

x = int(input())

line_number=1 #line number
output_count=0 #Number of character outputs in line

for i in range(1, x+1):
    print(i , end = ' ')
    output_count+=1

    if(line_number == output_count):
        print()
        line_number+=1
        output_count=0;


2022-09-20 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.