【问题标题】:Print the "multiple functions output" [not just printing Strings] in same line in Python在 Python 的同一行中打印“多个函数输出”[不仅仅是打印字符串]
【发布时间】:2020-04-28 11:45:40
【问题描述】:

所以我试图在同一行打印多个函数输出,而不是字符串,我知道我们可以在 python 3.0 及更高版本中使用 end=" 但这是不同的场景,我尝试迭代函数但没有用,我一直在尝试,但我想知道如何解决这个问题,我仍然是初学者,所以有任何帮助,还请检查我的代码和示例输出

class Costco:

 def __init__(self,n):
   self.n=n

 def letterC(self):
  print('*'*self.n)
  for i in range(self.n-2):
    print('*')
  print('*'*self.n)



 def letterO(self):
  print('*'*self.n)
  for i in range(self.n-2):
    print('*'+' '*(self.n-2)+'*')
  print('*'*self.n)


 def letterS(self):
  print('*'*self.n)
  for i in range(int(self.n/2)):
    print('*')
  print('*'*self.n)
  for i in range(int(self.n/2)):
    print(' '*(self.n-1)+'*')
  print('*'*self.n)


 def letterT(self):
  print('*'*self.n)
  for i in range(self.n-1):
    print(' '*int(self.n/2)+'*')


c1=Costco(int(input('please enter an odd number:\n')))
while c1.n%2==0:
   print('you entered an even number,please enter only odd number')
   c1=Costco(int(input('please enter an odd number:\n')))

def cost(c1):
  for i in [c1.letterC,c1.letterO,c1.letterS,c1.letterT,c1.letterC,c1.letterO]:
    i()


cost(c1)

所以示例输出看起来像

please enter an odd number:
7
*******
*
*
*
*
*
*******
*******
*     *
*     *
*     *
*     *
*     *
*******
*******
*
*
*
*******
      *
      *
      *
*******
*******
   *
   *
   *
   *
   *
   *
*******
*
*
*
*
*
*******
*******
*     *
*     *
*     *
*     *
*     *
*******

但我想要更多像在一行中 喜欢

COSTCO 

但不是

C
O
S
T
C
O

【问题讨论】:

    标签: python-3.x function object printing output


    【解决方案1】:

    该打印函数在您要求打印的任何内容的末尾插入一个换行符,因此每次调用 print() 时,您最终都会向下移动一层。解决这个问题的一种方法是将每个字符存储为一个字符串数组,每个字符串都包含您要打印的行。

    所以 C[0] 将是“*******”,c[1] 将是“*”,而 c[6] 将是“******”

    然后使用 for 循环一次打印一个字符的每一层,在每个组件之间插入一个空白字符。有点像 print(c[i]+o[i]+s[i]+t[i]+c[i]+o[i])

    唯一的问题是您必须事先逐层定义每个字符,并更改打印“S”的方式,使其与其余字符具有相同的层数。

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 2021-07-03
      • 2015-07-28
      • 2019-04-20
      • 2021-11-01
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      相关资源
      最近更新 更多