简体   繁体   中英

how can i achieve this ? I'm trying to have an Input that will print out all the numbers in sequential order using python

Desired Output This is what I want my output to be.

What I have now.

rows = int(input("Please Enter a Number  : "))

for I in range(1, rows + 1):

    for j in range(1, I + 1):

        print('*', end = '  ')

    print()

But This is what I want my output to be

Enter any number : 21
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

Right Angle triangle output thing.

Its been driving me nuts. Please help:) Thanks in anticipation

Hellor, here's your solution for your problem

rows = int(input("Please Enter a Number  : "))
count = 1
number = 1
while number <= rows:
    for i in range(count):
        print(number, end=" ")
        number += 1
    count += 1
    print("\n", end="")

Since you always need to add one number per line you need a variable to store the number of numbers to print (Here count ). You also need a variable to count the current number that you increment for each iteration of count (here number ) and you stop only when the current number is greater then the input (Here rows )

Ignoring the triangle part, the first step is to print all the numbers:

rows = int(input("Please Enter a Number  : "))
for num in range(1, rows + 1):
  print(num, end=" ")

From there we want to add an if to also print a new line if our number is

  • equal to 'rows', or
  • the number we expect to be at the end of the current level

Since the number of items in each row is an arithmetic progression (ie each level is +1 longer than the level above it) we can calculate the number at the end of each level using the formula for summing an arithmetic progression:

number_at_end_of_level = level * (level + 1) / 2

The simple explanation for this formula is that you're finding the average number of items in each level by averaging the first and last level average = (level + 1) / 2 . Because we know the average, we can then multiply that by the number of levels to get the total.

So putting it all together the solution is:

rows = int(input("Please Enter a Number  : "))
level = 1
for num in range(1, rows + 1):
  print(num, end=" ")
  number_at_end_of_level = level * (level + 1) / 2
  if num == number_at_end_of_level or num == rows:
    level += 1
    print()

Alternatively, if you don't want to do the calculation, you can just count how many items you've printed in the current level and print a new line when that count is equal to the current level number:

rows = int(input("Please Enter a Number  : "))
level = 1
current_level_count = 0
for num in range(1, rows + 1):
  print(num, end=" ")
  current_level_count += 1
  if current_level_count == level or num == rows:
    level += 1
    current_level_count = 0
    print()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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