【问题标题】:Trying to count from 10 to the input value provided and columns provided in Python but not getting it. I basically want like 5 numbers on top etc尝试从 10 数到提供的输入值和 Python 中提供的列,但没有得到它。我基本上想要5个数字等
【发布时间】:2020-05-27 12:11:45
【问题描述】:
counter = 10
numbers = int(input("Enter a number between 10 and 99: "))
column = int(input("How many columns would you like? "))
for num in range(10, numbers):
    for col in range(column):
        counter += 1
    print(num + 1, end= ' ')
print()

尝试从 10 数到提供的输入值和 Python 中提供的列,但没有得到它。我基本上想要顶部 5 个数字,底部 5 个等。

【问题讨论】:

  • 内部for循环中counter += 1的作用是什么?
  • 您能否为不同的输入添加一些示例,以了解您想要实现的目标?

标签: python python-3.x spyder juniper


【解决方案1】:

你想要这样的东西吗?

>>> n = 30 # numbers
>>> c = 3 # columns
>>> for i in range(10, n+1):
...     print(i, end='\t')
...     if (i - 10) % c == 0:
...         print()
... else:
...     print()
...
10      11      12
13      14      15
16      17      18
19      20      21
22      23      24
25      26      27
28      29      30

>>>

【讨论】:

  • 我相信 OP 想要 10 到 n,而不是 n+10
【解决方案2】:
counter = 10
numbers = int(input("Enter a number between 10 and 99: "))
column = int(input("How many columns would you like? "))
output_string = ""

col_counter = 0
while (counter <= numbers):
    output_string += str(counter)+" "
    counter += 1
    col_counter += 1
    if(col_counter == column):
       print(output_string)
       output_string=""
       col_counter = 0
print(output_string)

这样就可以了

【讨论】:

  • 这正是我所需要的。我想使用 for 循环,但这有效。
猜你喜欢
  • 1970-01-01
  • 2021-10-04
  • 2023-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多