绘制形状的功能

时间:2014-03-23 06:15:34

标签: python function drawing

程序中的主要功能应该命名为:drawShape()。

o主函数将提示两个值,即宽度和 高度。假设:

两个值都是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 :(得分:0)

  1. 使用'#'*width生成预期宽度为'#'的字符串。
  2. range(width, height)没有意义,只是range(height)(或者height/2)。
  3. 然后你可以这样做:

    In [368]: print('#'*width)
         ...: for i in range(height/2):
         ...:     print('+'*width)
         ...:     print('#'*width)
    ###
    +++
    ###
    +++
    ###
    +++
    ###
    
相关问题