首页 > 解决方案 > 连续打印一定次数的东西

问题描述

我正在制作一个 twitch 机器人,并希望有一个金字塔命令,该命令将在聊天中发送一个横向金字塔,如下所示 => https://i.imgur.com/pzKMH2i.png

目前它在一行中打印所有内容,但打印数量正确,但它应该是横向而不是直立的。

将 pyramid_width 更改为“a”可以修复它,但是它需要发送聊天中指定的单词。https://i.imgur.com/HP2eRoQ.png

我不确定如何实际解决此问题,因此将不胜感激<3

  def send_pyramid(width, text, channel):
        text = str(text) + " " if not str(text).endswith(" ") else str(text)
        while text.find('  ') != -1:
            text.replace('  ', ' ')
        for i in range(0, width + 1):
            sendcommand("/me " + i * text, channel)
        width -= 1
        while width != 0:
            sendcommand("/me " + width * text, channel)
            width -= 1


    def get_first_nbr_from_str(input_str):
        '''
        :param input_str: strings that contains digit and words
        :return: the number extracted from the input_str
        demo:
        'ab324.23.123xyz': 324.23
        '.5abc44': 0.5
        '''
        if not input_str and not isinstance(input_str, str):
            return 0
        out_number = ''
        for ele in input_str:
            if (ele == '.' and '.' not in out_number) or ele.isdigit():
                out_number += ele
            elif out_number:
                break
        return float(out_number)


    def clearup(s, chars):
        return re.sub('[%s]' % chars, '', s).lower()

    if "++test" in chat:
        echo = chat.replace("++test","")
        echo = str(echo)
        pyramid_width = (get_first_nbr_from_str(echo)) # finds the number
        pyramid_width = int(pyramid_width)
        pyramid_text = clearup(echo, string.punctuation+string.digits) # replaces number
        print(pyramid_text)
        for i in range(0, pyramid_width + 1):
            print(i * pyramid_text)
        pyramid_width -= 1
        while pyramid_width != 0:
            print(pyramid_width * pyramid_text)
            pyramid_width -= 1

标签: pythontwitch

解决方案


推荐阅读