我想编辑python代码以供用户输入

时间:2018-10-16 02:47:28

标签: python python-3.x

此程序打印金字塔图案。我想编辑此代码,以获取用户输入的不仅是*的任何字符。

  def triangle(n):

        k = 2*n-2
        for i in range(0, n):
            for j in range(0, k):
                print(end = ' ')

            k = k-1

            for t in range(0, i+1):
                print('*', end = " ")
            print("\r")



    def main():

        n = int(input("Input a number: "))
        triangle(n)



    main()

3 个答案:

答案 0 :(得分:3)

PerformanceWarning: Adding/subtracting array of DateOffsets to Series not vectorized "Series not vectorized"

答案 1 :(得分:1)

您应该为打印字符创建一个变量,也许叫my_char,使用raw_input()函数通过键盘从用户那里获取它,然后将该变量作为参数传递给函数triangle()。可能看起来像:

def triangle(n, my_char):

    k = 2*n-2
    for i in range(0, n):
        for j in range(0, k):
            print(end = ' ')

        k = k-1

        for t in range(0, i+1):
            print(my_char, end = " ")
        print("\r")


def main():
    n = int(input("Input a number: "))
    my_char = raw_input("Input a character: ")
    triangle(n, my_char[0])


main()

答案 2 :(得分:1)

n=int(raw_input('Enter number of lines '))
character = raw_input('Enter char to print: ')
for i in range(1,n+1):
   print ((n-i)*' '+i*(character +' '))  

您可以添加错误检查,以便用户仅输入正数