1

*からツリーを作成する必要があります。高さは、ユーザーが挿入する数で、追加の行のためにさらに2つ*、そしてその1/3の長さである必要があります。

さらに、最後の行は前にスペースがない必要があります。コード全体を作成しましたが、最後の最長の行が1つのスペースで表示されます。

どこが間違っているの?

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

これには文字列フォーマットを使用します:

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 に答える
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 に答える