1

我需要从 * 制作一棵树,高度需要是用户插入的数字,另外 2 个 * 用于额外的行,以及它的 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
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
       *
       *
4

3 回答 3

3

尝试改变:

print space*" ",i*"*"

经过:

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

当你这样做时:

print ' ','3'

输出不同于

print ' '+'3'
于 2012-10-28T18:28:41.510 回答
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 
       ##      ###                                              #########       
        .#####  .########                                 ######....  ...       
        ... ..#### ..   #                              ####  .  ....  ...       
        ... .. . .##### ######                         ....  .                  
             . . .    .# . . .###                     #....            ####     
      ######     .    .# . . . ..                    #..              ##...     
      ..  ..#          ##########                   ##.           ####.....     
      ..  ...##          . .   ..##                #. .          ## . ..        
           .. .#         . .   ....# ######        ..         ####  .           
            . ..######           ..# ##############.         #....              
               ..   ..###         .########    ...############....              
               ..   .. .#####     .########    ###.. . ...  . .                 
                     . .. . ######.#.###### ###. ..  . ...  .                   
                        . . .. .   #.########... .                              
                            .. .   ########  ..                                 
                                    #######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
                                     ######                                     
于 2012-10-28T18:52:43.113 回答
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
       *       
      ***      
     *****     
    *******    
   *********   
  ***********  
 ************* 
***************
       *       
       *       
于 2012-10-28T18:32:05.800 回答