[英]How to write the following pattern
I am trying to print the following pattern in python but am not getting the desired output :我正在尝试在 python 中打印以下模式,但没有得到所需的输出:
0
2 2
4 4 4
6 6 6 6
I have already used the following code but am not getting the desired output我已经使用了以下代码,但没有得到所需的输出
n = int (input("Enter number : "))
for i in range (n):
print (i *2 )
Try this:尝试这个:
n = int (input("Enter number : "))
for i in range (n):
print (' '.join([str(i * 2) for _ in range(i + 1)]) )
You need to print the number i+1
times.您需要打印数字
i+1
次。
for i in range (n):
for _ in range(i+1):
print (i * 2," ", end='') # print without newlines
print() # end the line
Following simple code could help:以下简单的代码可能会有所帮助:
n = int (input("Enter number : "))
j = 1
k = 0
for i in range(n):
s = str(k)+' '
print(s*j)
j += 1
k += 2
if n = 4
, then it would print the following :如果
n = 4
,那么它将打印以下内容:
0
2 2
4 4 4
6 6 6 6
n = int (input("Enter number : "))
count = 1
for i in range (n):
res = str(i*2) + " "
print (res * (i+1))
print n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.