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
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.
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;
© 2024 OneMinuteCode. All rights reserved.