Python金字塔字符打印错误

Python金字塔字符打印错误,python,Python,我试图让用户选择将用于构建金字塔的角色 sizePy=int(input('Enter number of lines: ')) charPy = raw_input("Enter the character: ") for i in range(1,sizePy+1): print ((sizePy-i)*' '+i*"%s " %charPy) 您的错误是由以下原因造成的: i*"%s " %charPy 应用*运算符后,例如,如果i为5,则这将是: "%s %s %s %s

我试图让用户选择将用于构建金字塔的角色

sizePy=int(input('Enter number of lines: '))
charPy = raw_input("Enter the character: ")

for i in range(1,sizePy+1):
    print ((sizePy-i)*' '+i*"%s " %charPy)

您的错误是由以下原因造成的:

i*"%s " %charPy
应用
*
运算符后,例如,如果i为5,则这将是:

"%s %s %s %s %s " %charPy
您只有一个夏比和五个夏比。要解决此问题,请将其更改为:

i*("%s " %charPy)

Python将首先执行
charPy
替换,然后重复该字符串
i
次。

如果您使用的是
Python3
,以下操作可能会起作用

sizePy=int(input('Enter number of lines: '))
charPy = input("Enter the character: ")

for i in range(1,sizePy+1):
    print ((sizePy-i)*' '+i*"{0}".format(charPy))

你说你犯了个错误。。。但是错误信息是什么?请将其包含在您的问题中。错误是回溯(最近一次调用last):文件“python”,第5行,在TypeError中:格式字符串的参数不足