为什么我在此代码中出现ImportError?

问题描述 投票:0回答:2
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)

导入错误:无法从'pyfiglet'导入名称'figlet_format'

python importerror
2个回答
0
投票

我在程序中看到一些问题。

1.install 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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.