简体   繁体   English

我怎样才能做到这一点? 我正在尝试使用 python 按顺序打印所有数字的输入

[英]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.所需的 Output这就是我希望我的 output 成为的样子。

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但这就是我想要的 output

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.直角三角形 output 东西。

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 ).由于您总是需要每行添加一个数字,因此您需要一个变量来存储要打印的数字数量(此处为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 )您还需要一个变量来计算为count的每次迭代增加的当前数字(此处number ),并且仅当当前数字大于输入时才停止(此处为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从那里我们想添加一个if来打印一个新行,如果我们的号码是

  • 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:由于每行中的项目数是一个算术级数(即每个级别比它上面的级别长+1),我们可以使用算术级数求和公式计算每个级别末尾的数字:

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 .这个公式的简单解释是,您通过平均第一个和最后一个级别的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()

暂无
暂无

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

相关问题 如何将多个 string.counts 添加到列表中? 我正在尝试使用元组打印数字 - How do I add multiple string.counts into a list? I'm trying to print the numbers out using tuples 如何查询我已输入的csv文件的特定列并使用python打印所有返回的行? - How can i query a specific column of a csv file that i have input and print all returned rows using python? 我正在尝试将数字放入列表并平方并全部打印出来 - I'm trying to put numbers into a list and square and print them all 我是 python 的新手,我想弄清楚如何在同一行上一次打印一个字母 - I'm new to python and I am trying to figure out how to print letters one at a time on the same line 我有一个 python 列表作为字符串,我想将它打印出来作为间隔和顺序 - I have a python list as strings and i want to print it out as spaced between and sequential 在 Python 中:如何让我的代码根据我的输入打印出我可以拼写的所有可能的单词? - In Python: How can i get my code to print out all the possible words I can spell based on my input? 使用for循环(Python)按顺序打印用户输入 - Print user input in sequential order using for loop (Python) 如何在python中按顺序附加两个列表? - How can I append two lists in sequential order in python? 我试图了解如何打印数组的所有可能组合 - I'm trying to understand how to print all the possible combinations of a array 如何打印所有结果? - How can I print out all the results?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM