【发布时间】:2021-10-27 23:10:28
【问题描述】:
这是来自 W3SCHOOLS 的代码。有没有办法让它以其他方式完成,也许没有'while'循环?
def histogram( items ):
for n in items:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)
histogram([2, 3, 6, 5])
【问题讨论】:
-
你可以在python中将字符串相乘。打印
*n 次:print('*' * n)。 -
@MichaelSzczesny,是否可以将字符串乘以列表中的每个值?
-
对不起,我认为从接受的答案中可以清楚地看到只是交换
print语句。我添加了完整解决方案的答案。 -
@MichaelSzczesny 应该很清楚,但我还是太新手,有时无法理解这些东西。非常感谢。
标签: python string list for-loop cycle