[英]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
来打印一个新行,如果我们的号码是
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.