无法在 else 中为模式打印定义逻辑
Not able to define logic in else for pattern printing
卡住了预期的图案打印
下面是我的代码,我已经打印了第一部分,但在打印其他部分时卡住了
需要一些输入如何为其他部分构建逻辑
n=29
breaking= int((n+1)/2)
lst = [ len(str(num).zfill(2)) * ' ' for num in reversed(range(1,n+1)) ]
cnt=-1
for row in range(1,n+1):
if row <= breaking:
lst[cnt]=str(row).zfill(2) + ' '
print(' '.join(lst))
cnt = cnt - 1
else:
预期输出:
01
02 01
03 02 01
04 03 02 01
05 04 03 02 01
06 05 04 03 02 01
07 06 05 04 03 02 01
08 07 06 05 04 03 02 01
09 08 07 06 05 04 03 02 01
10 09 08 07 06 05 04 03 02 01
11 10 09 08 07 06 05 04 03 02 01
12 11 10 09 08 07 06 05 04 03 02 01
13 12 11 10 09 08 07 06 05 04 03 02 01
14 13 12 11 10 09 08 07 06 05 04 03 02 01
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01
14 13 12 11 10 09 08 07 06 05 04 03 02 01
13 12 11 10 09 08 07 06 05 04 03 02 01
12 11 10 09 08 07 06 05 04 03 02 01
11 10 09 08 07 06 05 04 03 02 01
10 09 08 07 06 05 04 03 02 01
09 08 07 06 05 04 03 02 01
08 07 06 05 04 03 02 01
07 06 05 04 03 02 01
06 05 04 03 02 01
05 04 03 02 01
04 03 02 01
03 02 01
02 01
01
虽然很乱,但是很管用。请在评论中告诉我,我会解释。
for i in range(1, n//2 + 1):
print(" "*(n+1-2*i) + " ".join(["{:02d}".format(i - j) for j in range(i)]))
for i in range(n//2 + 1, 0, -1):
print(" "*(n+1-2*i) + " ".join(["{:02d}".format(i - j) for j in range(i)]))
Here's what you can do:
代码
n = int(input('Enter max value : '))
num_width = len(str(n))
seperation = ' '
tot_width = num_width * n + len(seperation) * (n-1)
for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
line = seperation.join([str.rjust(str(j), num_width, '0') for j in range(i, 0, -1)])
print(str.center(line, tot_width))
输出
说明
First for loop
iterates over max-value allowed in the current line
to be printed.
Second for loop
iterates over each number to be printed (i.e. from 1
to max-value in the current line
in reverse order.
代码逐行解释
n = int(input('Enter max value : ')) # Get the max value for the pattern
# Know the length of highest value in the pattern so that we can make all the digits
# in the pattern of that width by adding zeroes
# for eg. if max number is 15, width is 2, max number is 178, width is 3..
num_width = len(str(n))
# The seperation to be added between 2 digits
seperation = ' '
# calculating max width of the pattern (which occurs at the middlemost line)
tot_width = n * num_width + len(seperation) * (n-1)
# (n digits of 'num-width' length) + (n-1) number of seperations between them
# i will go initially from 1 to n, which is : list(range(1, n + 1))
# then will decrease from n to 1, which is : list(range(n - 1, 0, -1))
for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
digits = []
for j in range(i, 0, -1): # now j will iterate from current value of 'i' to '1' in decreasing order
# each digit in current line is created with a width of 'num-width' by adding preceding zeroes
digits.append(str.rjust(str(j), num_width, '0'))
# Finally, join all the digits with 'seperation' in between to create the line
line = seperation.join(digits)
print(str.center(line, tot_width)) # now center the line by adding spaces on both the sides, and print it
此代码普遍适用于 n
的任何值
卡住了预期的图案打印
下面是我的代码,我已经打印了第一部分,但在打印其他部分时卡住了 需要一些输入如何为其他部分构建逻辑
n=29
breaking= int((n+1)/2)
lst = [ len(str(num).zfill(2)) * ' ' for num in reversed(range(1,n+1)) ]
cnt=-1
for row in range(1,n+1):
if row <= breaking:
lst[cnt]=str(row).zfill(2) + ' '
print(' '.join(lst))
cnt = cnt - 1
else:
预期输出:
01
02 01
03 02 01
04 03 02 01
05 04 03 02 01
06 05 04 03 02 01
07 06 05 04 03 02 01
08 07 06 05 04 03 02 01
09 08 07 06 05 04 03 02 01
10 09 08 07 06 05 04 03 02 01
11 10 09 08 07 06 05 04 03 02 01
12 11 10 09 08 07 06 05 04 03 02 01
13 12 11 10 09 08 07 06 05 04 03 02 01
14 13 12 11 10 09 08 07 06 05 04 03 02 01
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01
14 13 12 11 10 09 08 07 06 05 04 03 02 01
13 12 11 10 09 08 07 06 05 04 03 02 01
12 11 10 09 08 07 06 05 04 03 02 01
11 10 09 08 07 06 05 04 03 02 01
10 09 08 07 06 05 04 03 02 01
09 08 07 06 05 04 03 02 01
08 07 06 05 04 03 02 01
07 06 05 04 03 02 01
06 05 04 03 02 01
05 04 03 02 01
04 03 02 01
03 02 01
02 01
01
虽然很乱,但是很管用。请在评论中告诉我,我会解释。
for i in range(1, n//2 + 1):
print(" "*(n+1-2*i) + " ".join(["{:02d}".format(i - j) for j in range(i)]))
for i in range(n//2 + 1, 0, -1):
print(" "*(n+1-2*i) + " ".join(["{:02d}".format(i - j) for j in range(i)]))
Here's what you can do:
代码
n = int(input('Enter max value : '))
num_width = len(str(n))
seperation = ' '
tot_width = num_width * n + len(seperation) * (n-1)
for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
line = seperation.join([str.rjust(str(j), num_width, '0') for j in range(i, 0, -1)])
print(str.center(line, tot_width))
输出
说明
First
for loop
iterates overmax-value allowed in the current line
to be printed.
Second
for loop
iterates over each number to be printed (i.e. from1
tomax-value in the current line
in reverse order.
代码逐行解释
n = int(input('Enter max value : ')) # Get the max value for the pattern
# Know the length of highest value in the pattern so that we can make all the digits
# in the pattern of that width by adding zeroes
# for eg. if max number is 15, width is 2, max number is 178, width is 3..
num_width = len(str(n))
# The seperation to be added between 2 digits
seperation = ' '
# calculating max width of the pattern (which occurs at the middlemost line)
tot_width = n * num_width + len(seperation) * (n-1)
# (n digits of 'num-width' length) + (n-1) number of seperations between them
# i will go initially from 1 to n, which is : list(range(1, n + 1))
# then will decrease from n to 1, which is : list(range(n - 1, 0, -1))
for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
digits = []
for j in range(i, 0, -1): # now j will iterate from current value of 'i' to '1' in decreasing order
# each digit in current line is created with a width of 'num-width' by adding preceding zeroes
digits.append(str.rjust(str(j), num_width, '0'))
# Finally, join all the digits with 'seperation' in between to create the line
line = seperation.join(digits)
print(str.center(line, tot_width)) # now center the line by adding spaces on both the sides, and print it