如何反转打印图案

时间:2015-10-07 07:31:07

标签: python python-3.x

我能够以缩小的方式打印出我的图案,但是如何以相反的方式将其打印出来以便扩展?

import time

pattSize = int(input("input pattern size: "))

while pattSize < 3:  
    pattSize = int(input("input pattern size: "))
    print("warning: pattern size should be at least 3!") 

if pattSize%2 == 0:
    steps = (2*(pattSize/2)) + 1;
else:
    steps = (2*((pattSize + 1) / 2))  + 1;

counter = 0
while True:
    step = (counter%steps)
    for i in range(pattSize):
        line = "";
        for j in range(pattSize):
            if (step <= 4):
                if (i == (0 + step) or j == (0 + step) ) and (i > (-1 + step) and j > (-1 + step)) and (i < (pattSize - step) and j < (pattSize - step)):
                    line += "*"
                elif (i == (pattSize - (1 + step)) or j == (pattSize - (1 +  step)) ) and (i > (-1 + step) and j > (-1 + step)) and (i < (pattSize - step) and j < (pattSize - step)):
                    line += "*"
                else:
                    line += "."

        print(line)

    counter += 1
    time.sleep(1);

对于5的输入,模式缩小如下。现在我想扭转它,这就是增长模式。

*****
*...*
*...*
*...*
*****
.....
.***.
.*.*.
.***.
.....
.....
.....
..*..
.....
.....
.....
.....
.....
.....
.....

1 个答案:

答案 0 :(得分:-1)

删除了一些不必要的代码。

  1. 为什么在寻找用户输入时突破while循环。
  2. if (step <= 4):
  3. 不需要柜台等。
  4. 代码:

    pattSize = int(input("input pattern size: "))
    
    while pattSize < 3:  
        print("warning: pattern size should be at least 3!") 
        pattSize = int(input("input pattern size: "))
    
    steps = int(math.ceil(pattSize/2)) 
    steps = list(range(steps))
    
    def pattern(steps):    
        for step in steps:
            for i in range(pattSize):
                line = ""
                for j in range(pattSize):
                        if (i == (0 + step) or j == (0 + step) ) and (i > (-1 + step) and j > (-1 + step)) and (i < (pattSize - step) and j < (pattSize - step)):
                            line += "*"
                        elif (i == (pattSize - (1 + step)) or j == (pattSize - (1 +  step)) ) and (i > (-1 + step) and j > (-1 + step)) and (i < (pattSize - step) and j < (pattSize - step)):
                            line += "*"
                        else:
                            line += "."
    
                print(line)    
            time.sleep(0.1)
    
    while True:
        pattern(steps)
        pattern(steps[1:-1][::-1])
    

    尝试使用50号尺寸看起来很静,看起来很酷:)