2 回答

TA贡献1860条经验 获得超9个赞
如果list是您的选择,这里有一个打印波浪的简单方法:
def print_wave(args):
total_cols = sum(args)
total_rows = args[0] # init total rows by first arg
matrix = [[' ' for _ in range(total_cols)] for _ in range(total_rows)]
curr_col = 0
curr_row = total_rows - 1
down = True
for num in args:
down = not down
char = '\\' if down else '/'
# update total rows and current row if need
if down:
diff = curr_row + num - total_rows
if diff > 0:
for _ in range(diff):
matrix.append([' ' for _ in range(total_cols)])
total_rows += diff
else:
diff = num - 1 - curr_row
if diff > 0:
for _ in range(diff):
matrix.insert(0, [' ' for _ in range(total_cols)])
total_rows += diff
curr_row += diff
for i in range(num):
matrix[curr_row][curr_col] = char
# for conjection not update curr row for the last loop
if i != num - 1:
if down:
curr_row += 1
else:
curr_row -= 1
curr_col += 1
for row in matrix:
for col in row:
print(col, end='')
print()
简单测试args=[3, 5, 4, 11, 2, 5, 7]:
/\
/ \ /\
/ \ / \
\ / \
\/ \
\
\
\
\ /
\ /
\ /\ /
\/ \ /
\ /
\ /
\/
希望这会有所帮助。

TA贡献1798条经验 获得超3个赞
当它是 python 2x 时,这很简单,它是由末尾的一个简单逗号完成的魔术,例如。Print("a"), Print("b") 输出:ab Incase of python 3x
你必须使用。end=" " 控制下一个打印函数之间的空格
添加回答
举报