简体   繁体   English

python星号三角形不使用y *“*”

[英]python asterisk triangle without using y* "*"

I'm trying to create a iso triangle (one that starts in the middle).我正在尝试创建一个 iso 三角形(一个从中间开始)。 I have a code but the problem is that I'm not allowed to use Y* "*" 5 in my code.我有一个代码,但问题是我不允许在我的代码中使用 Y* "*" 5。 (The y is a variable there) Also I may only use one print statement at the end of my code. (y 是那里的一个变量)我也可能只在我的代码末尾使用一个打印语句。 Can you please help me out.你能帮我一下吗。

f = int(raw_input("enter"))
for i in range(f):
    print " " * (f-i-1) + "*" * (2*i+1)

creats this triangle创建这个三角形

     *
    ***
   *****
  *******
 *********
***********




However, you are not allowed to use the *-operator on string and int.但是,不允许在 string 和 int 上使用 *-operator。 So for example ''***'' * 3 is not allowed, but 3 * 4 is因此,例如 ''***'' * 3 是不允许的,但 3 * 4 是允许的

This just creates a continuous string and then prints it at the end这只是创建一个连续的字符串,然后在最后打印它

f = int(raw_input("Enter height: "))
s = ''
for i in xrange(f):
    for j in xrange(f-i-1):
        s += ' '
    for j in xrange(2*i+1):
        s += '*'
    s += '\n'
print s

This is a solution which i think is very easy to understand.这是一个我认为很容易理解的解决方案。 You can make the parameter of range() variable, to make it more dynamic.您可以使range()的参数可变,以使其更具动态性。

from __future__ import print_function

for i in range(1,12,2):
    count=(11-i)/2
    for j in xrange(count):
        print(" ",end='')
    for j in xrange(i):
        print("*",end='')
    for j in xrange(count):
        print(" ",end='')
    print(end="\n")

I think the best solution is using the center() string method:我认为最好的解决方案是使用center()字符串方法:

f = int(raw_input("How many rows to print in the triangle? "))
star = "*"
full_string = ""
for X in xrange(f):
    star += "**" if X>0 else ""
    full_string += star.center(2*f-1) + "\n"
print full_string[:-1]

The str.center() documentation: str.center()文档:

https://docs.python.org/2/library/string.html#string.center https://docs.python.org/2/library/string.html#string.center


EDIT: If you can't use the print statement within the for loop, you could concatenate the string during the loop and print it at the end:编辑:如果您不能在for循环中使用print语句,您可以在循环期间连接字符串并在最后打印它:

f = int(raw_input("How many rows to print in the triangle? "))
star = "*"
full_string = ""

for X in xrange(f):
    # the first row should take only one star
    star += "**" if X>0 else ""
    star2 = star.center(2*f-1)
    full_string += star2 + "\n"

# slice the string to delete the last "\n"
print full_string[:-1]

I noticed that using a for loop add a newline character.我注意到使用for循环添加换行符。 If you want to avoid this, you can slice the string before printing.如果你想避免这种情况,你可以在打印前对字符串进行切片。

There is no problem with this code, i just checked it and it worked fine.这段代码没有问题,我只是检查了它,它工作正常。 If you would post the error message we might be able to help a bit more.如果您发布错误消息,我们可能会提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM