英文:
Triangle pattern printing in Python
问题
max = 37
max2 = max
no_of_lines = 0
subtractor = 1
while max >= 1:
no_of_lines += 1
max = max - subtractor
subtractor += 1
counter = 1
for i in range(no_of_lines):
print((no_of_lines - i) * " " + (no_of_lines - i - 1) * " ", end="")
# the next loop is to create the string of numbers in a line
s = ""
for k in range(i + 1):
if (counter > max2):
s = " *" + s
else:
s = " " + str(counter) + s
counter += 1
print(s)
英文:
Write a program that prompts the user to enter the maximum value of range and display the values in the triangle format and use any notation for the white space to make a complete triangle.Example:Enter the maximum range: 37
This is the solution I came up with. But the space printed is incorrect. Can anyone help?
max = 37
max2 = max
no_of_lines = 0
subtractor = 1
while max>=1:
no_of_lines+=1
max = max-subtractor
subtractor +=1
counter = 1
for i in range(no_of_lines):
print((no_of_lines-i)*" " + (no_of_lines-i-1)*" ", end="")
# the next loop is to create the string of numbers in a line
s = ""
for k in range(i+1):
if(counter>max2):
s = " "+"*"+s
else:
s = " "+str(counter)+s
counter+=1
print(s)
答案1
得分: 2
确保任何N值(在OP的原始代码中被误称为max)的正确对齐都很麻烦。问题中有缺陷的代码暗示N将始终是两位数,但这可能并非一定如此。
尽管更复杂,但这种方法适用于多位数的N:
N = 37
v = i = 1
t = []
while v <= N:
t.append([])
for _ in range(i):
t[-1].append(v)
if (v := v + 1) > N:
break
i += 1
m = 1 if N == 1 else len(t[-2])+1
w = len(str(N))
for i, _t in enumerate(t, 1):
line = [f'{v:>{w}}' for v in _t]
if (pad := m - len(line)) > 0:
fill = '*' if i == len(t) else ' '
line += [f'{fill:>{w}}' for _ in range(pad)]
print(' '.join(reversed(line)))
输出:
1
3 2
6 5 4
10 9 8 7
15 14 13 12 11
21 20 19 18 17 16
28 27 26 25 24 23 22
36 35 34 33 32 31 30 29
* * * * * * * * 37
英文:
Ensuring proper alignment for any value of N (spuriously named max in OP's original code) is awkward. The flawed code in the question implies that N will always be a 2-digit value but that may not necessarily be the case.
This approach, albeit more complex, handles multi-digit values for N:
N = 37
v = i = 1
t = []
while v <= N:
t.append([])
for _ in range(i):
t[-1].append(v)
if (v := v + 1) > N:
break
i += 1
m = 1 if N == 1 else len(t[-2])+1
w = len(str(N))
for i, _t in enumerate(t, 1):
line = [f'{v:>{w}}' for v in _t]
if (pad := m - len(line)) > 0:
fill = '*' if i == len(t) else ' '
line += [f'{fill:>{w}}' for _ in range(pad)]
print(' '.join(reversed(line)))
Output:
1
3 2
6 5 4
10 9 8 7
15 14 13 12 11
21 20 19 18 17 16
28 27 26 25 24 23 22
36 35 34 33 32 31 30 29
* * * * * * * * 37
答案2
得分: 1
这是使您的代码运行所需的调整。顺便说一下,查看rjust
函数的建议很好。
max = 37
max2 = max
no_of_lines = 0
subtractor = 1
while max >= 1:
no_of_lines += 1
max = max - subtractor
subtractor += 1
counter = 1
for i in range(no_of_lines):
print((no_of_lines - i) * " " + (no_of_lines - i - 1) * " ", end="")
# 下一个循环是创建单行数字字符串
s = ""
for k in range(i + 1):
if counter > max2:
s = " " + " *" + s
else:
s = " " + "%2d" % counter + s
counter += 1
print(s)
输出:
1
3 2
6 5 4
10 9 8 7
15 14 13 12 11
21 20 19 18 17 16
28 27 26 25 24 23 22
36 35 34 33 32 31 30 29
* * * * * * * * 37
英文:
Here are the tweaks needed to make your code work. BTW, the advice to look at the rjust
function is good advice.
max = 37
max2 = max
no_of_lines = 0
subtractor = 1
while max>=1:
no_of_lines+=1
max = max-subtractor
subtractor +=1
counter = 1
for i in range(no_of_lines):
print((no_of_lines-i)*" " + (no_of_lines-i-1)*" ", end="")
# the next loop is to create the string of numbers in a line
s = ""
for k in range(i+1):
if(counter>max2):
s = " "+" *"+s
else:
s = " "+"%2d"%counter+s
counter+=1
print(s)
Output:
1
3 2
6 5 4
10 9 8 7
15 14 13 12 11
21 20 19 18 17 16
28 27 26 25 24 23 22
36 35 34 33 32 31 30 29
* * * * * * * * 37
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论