Python 使用 for 循环创建圣诞树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26555986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:39:10  来源:igfitidea点击:

Using for loops to create a christmas tree

pythonfor-looppython-3.x

提问by Sam Procter

I am trying to create a program where you enter a number and the program creates a "christmastree" arrangement of +'s. For example if I enter the number 5 the program should print:

我正在尝试创建一个程序,您可以在其中输入一个数字,然后该程序会创建一个 + 的“圣诞树”排列。例如,如果我输入数字 5,程序应该打印:

    +
   +++
  +++++
 +++++++
+++++++++

What I have so far is:

到目前为止我所拥有的是:

def holidaybush(n):
    z=n-1
    x=1
    for i in range(0,n):
        for i in range(0,z):
            print('',end='')
        for i in range(0,x):
            print('+',end='')
        for i in range(0,z):
            print('',end='')
        x=x*2
        x=x-1
        z=z-1
        print()
holidaybush(5)

It does not work quite the way I expect, even though I go through the logic and it seems to work in my head. Any help? I just learned for loops today so I may not know everything about them.

它并不像我期望的那样工作,即使我通过了逻辑并且它似乎在我的脑海中工作。有什么帮助吗?我今天刚刚学习了 for 循环,所以我可能不了解它们的一切。

采纳答案by matsjoyce

OK, you have two problems. First, when you go to do your indentation, you write:

好的,你有两个问题。首先,当你进行缩进时,你写:

print('',end='')

In python (and other languages), ''is an empty string. You should use ' '.

在python(和其他语言)中,''是一个空字符串。你应该使用' '.

Second, your xincrementing logic seems to be wrong. Simply adding 2each loop works fine, making your program:

其次,你的x递增逻辑似乎是错误的。只需添加2每个循环就可以正常工作,使您的程序:

def holidaybush(n):
    z=n-1
    x=1
    for i in range(0,n):
        for i in range(0,z):
            print(' ',end='')
        for i in range(0,x):
            print('+',end='')
        for i in range(0,z):
            print(' ',end='')
        x=x+2
        z=z-1
        print()
holidaybush(5)

Your code can be made more compact by:

您的代码可以通过以下方式变得更紧凑:

  • Using infix operators, replacing x=x+2with x+=2
  • rangeautomatically starts at zero, so range(0,z)can be replaced with range(z)
  • Using string multiplication, replacing your inner forloops with ' ' * z
  • 使用中缀运算符,替换x=x+2x+=2
  • range自动从零开始,因此range(0,z)可以替换为range(z)
  • 使用字符串乘法,for' ' * z

Applying these results in:

将这些结果应用于:

def holidaybush(n):
    z = n - 1
    x = 1
    for i in range(n):
        print(' ' * z + '+' * x + ' ' * z)
        x+=2
        z-=1
holidaybush(5)

But you might want to stick with the verbose version.

但您可能想坚持使用详细版本。

回答by Ejaz

how about this:

这个怎么样:

def holidaybush(n):
    for i in range(n):
        print ' ' * (n - (i + 1)),'+' * (2*i+1)

holidaybush(5)

回答by Alex J Watts

If you change the 'x+=2' to 'x+=1' you get the right shape, but not as many '+', but it won't be as wide mod:

如果您将 'x+=2' 更改为 'x+=1',您会得到正确的形状,但没有那么多的 '+',但它不会那么宽:

def holidaybush(n):
z = n 
x =1

for i in range(n):
    print(' ' * z + '+' * x + ' ' * z)
    x+=1
    z-=1


holidaybush(5)

回答by Strap

You could use string.center(), just for adding another solution, this made the code more compact:

您可以使用string.center(), 只是为了添加另一个解决方案,这使代码更加紧凑:

def holidaybush(n):
    for i in range(n):
        print(("+" * (i * 2 + 1)).center(n * 2 - 1))

holidaybush(5)

回答by user9156169

height = eval(input("Enter height of tree: "))
for row in range(height):
    for count in range(height - row):
        print(end=" ")
    for count in range(2*row + 1):
        print(end="*")
    print()

回答by Chuanyui Teh

We can also do it without any numbers, just for fun

我们也可以不用任何数字来做,只是为了好玩

n = int(input("how big?\t"))
for i in range(n):
    for j in range(n-i):
        print(' ', end='')
    print('\b', end='') #cheating
    for k in range(i+n-j):
        print('*', end='')
    print('')

回答by TT Chowder

number = -1
Range = int(input("how many layers do you want the tree?"))
for x in range(0,Range):
    number = number + 2
    print(" " * (Range - x), "+" * number)

This worked for me.

这对我有用。

回答by danish hasan

`

`

a=7
n=7
for i in range(7):
  print(" "*a,end="")
  print("*"*(2*i+1))
  a-=1
for b in range (n//2):
  print("     *****")

` try this simple code to print the tree using for loop

` 试试这个简单的代码来使用 for 循环打印树

回答by user8334196

This is the very simplest code to draw Christmas tree:

这是绘制圣诞树的最简单代码:

for i in range(1,20,2):
    print(('*'*i).center(20))

for leg in range(3):
    print(('||').center(20))

print(('\====/').center(20))