打印一定数量的字符

时间:2018-06-04 18:09:07

标签: python pyscripter

这是针对学校的一个项目,我在python中编码很新。

基本上,我正在制作一个打印一定数量的X和空格的程序,例如X X X X X... 但是,我希望用户选择他们想要打印的X&X和空格。

这是我迄今为止所做的一切:

xnum = input("Choose the amount of 'X's you would like to be printed")
xnum = xnum.upper()
spacenum = input("Choose the amount of spaces you would like to be printed")
linenum = input("Choose how many characters you would like to be displayed on one line")

我想了解如何让程序打印用户要求的X的数量,并且与空格相同。

PS:我已经有了每行多少个字符的代码,我只需要帮助打印一定数量的' X和空格。

1 个答案:

答案 0 :(得分:0)

此代码假设您的意思是“X' X'而不是空间。

xnum = int(input("Choose the amount of 'X's you would like to be printed\n"))
spacenum = int(input("Choose the amount of spaces you would like to be printed\n"))
linenum = int(input("Choose how many characters you would like to be displayed on one line\n"))

for i in range(xnum):
    if(i%linenum==0 and i!=0):
        print("")
    print("X", end="")
    if(spacenum!=0):
        print(" ", end="")
        spacenum -= 1