python Colorama不会停止打印彩色文本

sgtfey8w  于 2023-06-04  发布在  Python
关注(0)|答案(2)|浏览(128)

我遇到了一个打印问题,关于我在colorama,Visual Studio Code,Python中的Fores。Fores(绿色)在我打印出的代码的每一个部分都打印GREEN,包括我不希望它显示的部分。下面是我的代码和代码的输出:

import colorama
from colorama import Fore
colorama.init()


print("let´s play!")

answer = input("What does CPU stand for?: ")

if answer == "Central Processing Unit":
    print(Fore.GREEN + "Correct")
else: 
    print(Fore.RED + "Incorrect, game will now crash")
    quit()

answer = input("What does RAM stand for?: ")

if answer == "Random Access Memory":
    print(Fore.GREEN + "Correct")
else: 
    print(Fore.RED + "Incorrect, game will now crash")
    quit()

answer = input("What does USB stand for?: ")

if answer == "Universal Serial Bus":
    print(Fore.GREEN + "Correct")
else: 
    print(Fore.RED + "Incorrect, game will now crash")
    quit()

print(Fore.GREEN + "Congratulations, you survived the game and won")
OUTPUT: 
let´s play!
What does CPU stand for?: Central Processing Unit
Correct (GREEN)
What does RAM stand for?: Random Access Memory (GREEN)
Correct (GREEN)
What does USB stand for?: Universal Serial Bus (GREEN)
Correct (GREEN)

正如你所看到的,绿色遵循我从第一个“正确”打印出来的一切。我只希望绿色显示时,打印正确,因为你可以看到我写了“打印(前。绿色+“正确”)”的每一个“正确”。然而,我不希望绿色出现在我的问题,因为它确实,从我的第一个“打印(前。绿色+“正确”)”的绿色不断出现在我打印的一切。
有什么办法解决这个问题吗?如何在打印时停止绿色(Fore.GREEN)?

nhaq1z21

nhaq1z211#

您需要在每个文本后使用Fore.RESET重置颜色,或者您可以在init函数中设置autoreset=True

tktrz96b

tktrz96b2#

这对我很有效

print(Fore.GREEN + "Correct" + Fore.WHITE)

+ Fore.YOUR_TERMINAL_TEXT_COLOR

相关问题