Python彩色文本切换问题

时间:2018-05-13 00:19:09

标签: python

您好我试图制作一个python脚本来循环文本并切换它。我能够让python在文本中切换一次,但我不能让它做的是继续翻阅文本。在我收到一条说明

的消息后,它会翻阅文本

回溯(最近一次呼叫最后):文件" test.py",第24行,在hello()文件" test.py",第22行,在hello hello()中TypeError:' str'对象不可调用

import time, sys, os 
from colorama import init
from termcolor import colored

def hello():
    os.system('cls')
    init()

    hello = '''Hello!'''

    print(colored(hello,'green',))
    time.sleep(1)
    os.system('cls')

    print(colored(hello,'blue',))
    time.sleep(1)
    os.system('cls')

    print(colored(hello,'yellow',))
    time.sleep(1)
    os.system('cls')
    hello()

hello()

3 个答案:

答案 0 :(得分:0)

您正在使用hello函数中名为hello的字符串覆盖hello函数。您可以尝试将hello字符串重命名为greeting,然后使用该字符串。

答案 1 :(得分:0)

您使用名称hello用于两个不同的目的,它们是冲突的。

您可以将它用于函数名称和函数中的变量。在其使用的任何地方更改其中一个的名称,然后重试。

答案 2 :(得分:0)

你有一个变量和一个名为hello的函数,所以在函数hello的范围内被覆盖。这可以通过重命名字符串轻松修复:

import time, sys, os
from colorama import init
from termcolor import colored

def hello():
    os.system('cls')
    init()

    greeting = '''Hello!'''

    print(colored(greeting,'green',))
    time.sleep(1)
    os.system('cls')

    print(colored(greeting,'blue',))
    time.sleep(1)
    os.system('cls')

    print(colored(greeting,'yellow',))
    time.sleep(1)
    os.system('cls')
    hello()

hello()