为什么我在这段代码中变得恐怖?

2024-05-02 01:38:26 发布

您现在位置:Python中文网/ 问答频道 /正文

import pyfiglet
from termcolor import colored

total_colors = ["red", "green", "yellow", "blue", "magenta", "cyan", "white"]

msg = input("What would you like to print?  ")
col = input("what color?")

if col not in total_colors:
    col = "green"

ascii_art = pyfiglet.figlet_format(msg)
colored_ascii = colored(ascii_art, color=col)
print(colored_ascii)

ImportError: cannot import name 'figlet_format' from 'pyfiglet'


Tags: fromimportinputasciimsgcolgreencolor
2条回答

我知道这听起来很明显,但请确保在您的计算机上安装了lib。如果是,请确保将lib安装在Python的安装位置

我在你的节目中看到了一些问题

1.安装pyfiglet模块
pip install pyfiglet

2.msg = input("What would you like to print? ")
不需要在这一行输入,因为下一行接受输入,程序的逻辑基于下一个输入

3.ascii_art = pyfiglet.figlet_format(msg) colored_ascii = colored(ascii_art, color=col) print(colored_ascii)

上面的一行应该是

ascii_art = pyfiglet.figlet_format(col) colored_ascii = colored(ascii_art, color=col) print(colored_ascii)

我安装了pyfiglet模块并实现了修改后的程序

import pyfiglet
from termcolor import colored

total_colors = ["red", "green", "yellow", "blue", "magenta", "cyan", "white"]

msg = print("What would you like to print? ")
col = input("what color?")

if col not in total_colors: col = "green"

ascii_art = pyfiglet.figlet_format(col)
colored_ascii = colored(ascii_art, color=col)
print(colored_ascii)

输出:

What would you like to print? 
what color?blue
[34m _     _            
| |__ | |_   _  ___ 
| '_ \| | | | |/ _ \
| |_) | | |_| |  __/
|_.__/|_|\__,_|\___|

[0m

相关问题 更多 >