开发者

Patterns in Python

开发者 https://www.devze.com 2023-03-16 05:20 出处:网络
This is in python language. I need to print this pattern, depending on the number of lines a user enters:

This is in python language. I need to print this pattern, depending on the number of lines a user enters:

'##'

'# #'

The next pattern is three spaces away and so on...

This is my code but it prints the second number 开发者_Python百科sign on a different line. I need them to go on the same line.

 def drawPatternIII (lines):
   for i in range(lines):
      for j in range(1):
          print('#')
      for j in range(i+1):
          print (' ', end = '')
      print ('#')

please help!


You would need to add end="" to the first print() call. While you are at it, remove the pointless for loop with a single iteration.

A concise alternative would be

for i in range(lines):
    print("#" + " "*i + "#")


The one liner version!

def drawPatternIII (lines):
     list(map(print,["#" + " "*i + "#" for i in range(lines)]))
0

精彩评论

暂无评论...
验证码 换一张
取 消