如何基于2个用户输入来创建数字行/列?

时间:2020-01-26 05:06:49

标签: python python-3.x

假设我有两个输入:

input1 = int(input("enter number")) #Lets give the example 5 right now
input2 = int(input("enter 2nd number"))#Lets give the example 6 right now

我要打印结果:

1 1 1 1 1    #5 columns, and 6 rows
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6

我正在考虑使用嵌套的for循环:

for i in range(input1):
   for j in range(input2):
       #rest of code

但是我不确定其余如何写。任何帮助将非常感激!预先谢谢你!

2 个答案:

答案 0 :(得分:5)

示例:

for row in range(5):
    for col in range(6):
        print(f"{row}", end=" ")
    print("")  # A newline

结果:

0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4

->您仍然必须对其进行调整,以便所有细节均正确。例如,Python是0索引。但是您可以轻松解决此问题。

答案 1 :(得分:2)

您可以通过单个for循环获取此

for i in range(1,input2+1): 
   print(" ".join([str(i)]*input1))