绘制形状的函数
你程序里的主函数应该叫做:drawShape()。
这个主函数会要求输入两个值,分别是宽度和高度。假设:
这两个值都要大于或等于2(不需要检查错误)
第二个输入(高度)必须是偶数
o 创建的形状宽度会和用户输入的一样。
o 形状的总高度会比用户输入的多一行。
o 不管高度是多少,形状的一半会在一行加号的上面,另一半会在下面。
def drawShape():
width = int(input("Please enter the shape width: "))
height = int(input("Please enter the shape height: "))
print("#")
for i in range(width, height):
print("#")
print("+")
return
预期输出:
drawShape()
Please enter the shape width: 4
Please enter the shape height: 2
####
++++
####
我得到的输出:
drawShape()
Please enter the shape width: 3
Please enter the shape height: 6
#
#
+
#
+
#
+
我在打印正确的宽度和高度时遇到了问题。有人能帮帮我吗!!!
1 个回答
0
- 用
'#'*width
这个方法可以生成一个指定宽度的'#'
字符串。 range(width, height)
这个写法不太对,应该直接用range(height)
(或者在你的情况下用height/2
)。
然后你可以这样做:
In [368]: print('#'*width)
...: for i in range(height/2):
...: print('+'*width)
...: print('#'*width)
###
+++
###
+++
###
+++
###