Python - 绘制树HW

时间:2012-10-28 18:08:35

标签: python

我需要从*创建一个树,高度需要是用户插入的数字,其中2个*用于额外的行,而brunk是其中的1/3。

此外,最后一行之前必须没有空格。 我制作了整个代码,但是最后一行和最长的行在...之前出现了一个空格。

我在哪里?

print "Enter tree height:"
height=input()
i=1
space=height-1
while i<=2*height:


    print space*" ",i*"*"
    i=i+2
    space=space-1

trunk=height/3
j=0

while j<trunk:
    print (height-1)*" ","*"
    j=j+1

输出:

Enter tree height: 3
  *
 ***
*****
  *
Enter tree height: 8
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
       *
       *

3 个答案:

答案 0 :(得分:3)

尝试改变:

print space*" ",i*"*"

由:

print (space)*" " + i*"*"

当你这样做时:

print ' ','3'

输出不同于

print ' '+'3'

答案 1 :(得分:0)

我会使用字符串格式:

print "Enter tree height:"
height=input()
max_width=((height-1)*2)+1

for i in range(1,max_width+1,2):
    print "{0:^{1:}}".format("*"*i,max_width)  # ^ to indent the code in center
                                               # as per  max_width

trunk=height/3

for i in range(trunk):
    print "{0:^{1:}}".format("*",max_width)

<强>输出:

monty@monty-Aspire-5050:~$ python so27.py
Enter tree height:
3
  *  
 *** 
*****
  *  
monty@monty-Aspire-5050:~$ python so27.py
Enter tree height:
8
       *       
      ***      
     *****     
    *******    
   *********   
  ***********  
 ************* 
***************
       *       
       *       

答案 2 :(得分:0)

这就是我的所作所为:

#!/usr/bin/env python3
import math
import random


class Tree(object):
    def __init__(self, height=24, width=80, branches=5):
        self.height = height
        self.width = width
        self.branches = branches

    def draw(self):
        tree = [[" " for i in range(self.width)] for j in range(self.height)]

        # These are arbitrary trunk dimensions, you can change them
        trunk = {}
        trunk["width"] = math.floor(self.width / 12)
        trunk["height"] = math.floor(self.height * (2 / 3))

        # Center the trunk. (0, 0) is the top-left corner of the screen.
        trunk["x"] = int(math.floor(self.width / 2) - math.floor(trunk["width"] / 2))
        trunk["y"] = int(self.height - trunk["height"])

        for i in range(trunk["height"]):
            for j in range(trunk["width"]):
                tree[i + trunk["y"]][j + trunk["x"]] = "#"

        # Add branches
        for i in range(self.branches):
            # Choose a position on the side of the trunk
            branch = {}
            if random.randint(0, 1):
                side = -1
                branch["x"] = trunk["x"] - 1
            else:
                side = 1
                branch["x"] = trunk["x"] + trunk["width"]

            branch["y"] = random.randint(1, math.floor(trunk["height"] / 2)) + (self.height - trunk["height"]) - 1

            tree[branch["y"]][branch["x"]] = "#"

            # Just sort of squiggle around for a while
            branch["length"] = math.floor(self.width / 2)
            for i in range(branch["length"]):
                direction_determiner = random.randint(0, 10)
                direction = {}
                if direction_determiner < 8:
                    direction["x"] = side
                    direction["y"] = 0
                else:
                    direction["x"] = 0
                    direction["y"] = -1

                branch["x"] += direction["x"]
                branch["y"] += direction["y"]

                if not (0 <= branch["x"] < self.width) or not (0 <= branch["y"] < self.height):
                    break

                tree[branch["y"]][branch["x"]] = "#"

                # Add a leaf sometimes
                if branch["y"] < self.height - 2 and random.randint(0, 1):
                    tree[branch["y"] + 1][branch["x"]] = "."
                    tree[branch["y"] + 2][branch["x"]] = "."

        # Draw the tree
        for i in range(self.height):
            for j in range(self.width):
                print(tree[i][j], end="")
            print("")


if __name__ == "__main__":
    tree_drawer = Tree()
    tree_drawer.draw()

它并不完美,但它可以输出这样的东西:

~/tmp $ ./tree.py 
       ##      ###                                              #########       
        .#####  .########                                 ######....  ...       
        ... ..#### ..   #                              ####  .  ....  ...       
        ... .. . .##### ######                         ....  .                  
             . . .    .# . . .###                     #....            ####     
      ######     .    .# . . . ..                    #..              ##...     
      ..  ..#          ##########                   ##.           ####.....     
      ..  ...##          . .   ..##                #. .          ## . ..        
           .. .#         . .   ....# ######        ..         ####  .           
            . ..######           ..# ##############.         #....              
               ..   ..###         .########    ...############....              
               ..   .. .#####     .########    ###.. . ...  . .                 
                     . .. . ######.#.###### ###. ..  . ...  .                   
                        . . .. .   #.########... .                              
                            .. .   ########  ..                                 
                                    #######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######