【发布时间】:2013-11-05 08:42:35
【问题描述】:
教授给了我们一个执行正方形的简单代码,我们需要添加/更改代码以输出如下所示的直角三角形形状。这只是循环代码中的一个简单循环,但如果代码看起来非常混乱/困难,我无法在任何地方找到使用 Python 创建形状的提示或帮助。我需要一个简单的解释来做什么以及为什么我需要做出这些改变。
(在 Python 中创建直角三角形的嵌套循环代码)
给出的执行方块的代码:
画正方形
size = input('Please enter the size: ')
chr = raw_input('Please enter the drawing character: ')
row = 1
while row <= size:
# Output a single row
col = 1
while col <= size:
# Output a single character, the comma suppresses the newline output
print chr,
col = col + 1
# Output a newline to end the row
print ''
row = row + 1
print ''
我需要输出的形状.....
x
x x
x x x
x x x x
x x x x x
x x x x x x
x x x x x x x
再次,只是简单的代码解释,是Python课程的介绍。
【问题讨论】:
-
如果你的代码确实有效,但又想改进,这个问题应该迁移到Code Review;如果它不起作用,请说明您预期会发生什么以及这与实际结果有何不同。请参阅 How to Ask 以获取有关什么是好的解释的提示。
标签: python loops nested geometry