Frank, Ian and Glen’s Letters
FIGlet, named after Frank, Ian, and Glen’s letters, is a program from the early 1990s for making large letters out of ordinary text, a form of ASCII art:
_ _ _ _ _ _
| (_) | _____ | |_| |__ (_)___
| | | |/ / _ \ | __| '_ \| / __|
| | | < __/ | |_| | | | __ \
|_|_|_|____| __|_| |_|_|___/
Among the fonts supported by FIGlet are those at figlet.org/示例.html.
FIGlet has since been ported to Python as a module called pyfiglet.
In 名为 figlet.py, 实现一个 program that:
- Expects zero or two 命令行参数s:
- Zero if the user would like to output text in a random font.
- Two if the user would like to output text in a specific font, in which case the first of the two should be
-for--font, and the second of the two should be the名字 of the font.
- Prompts the user for a
strof text. - Outputs that text in the desired font.
If the user provides two 命令行参数s and the first is not -f or --font or the second is not the名字 of a font, the program should exit via sys.exit with an error message.
提示
- You can install
pyfigletwith:pip install pyfiglet - The documentation for pyfiglet isn’t very clear, but you can use the module as follows:
from pyfiglet import Figlet figlet = Figlet()You can then get a
listof avai实验le fonts with code like this:figlet.getFonts()You can set the font with code like this, wherein
fis the font’s名字 as astr:figlet.setFont(font=f)And you can output text in that font with code like this, wherein
sis that text as astr:print(figlet.renderText(s)) - 笔记 that the
randommodule comes with quite a few functions, per docs.python.org/3/library/random.html.
演示
This demo’s first output used a random font. Your output 五月 vary.
Before You Begin
- Log into cs50.dev using your GitHub account.
- Click inside the terminal window and execute
cd. - Execute
wget https://cdn.cs50.net/2022/fall/实验s/6/figlet.zipfollowed by Enter in order to 下载 a zip calledfiglet.zipin your codespace. Take care not to overlook the space betweenwgetand the following URL, or any other character for that matter! - Now execute
unzip figlet.zipto create 的文件夹中名为figlet. - You 不再 need the ZIP file, so you can execute
rm figlet.zipand respond with “y” followed by Enter at the prompt.
如何测试
Here’s how to test your code manually:
- Run your program with
python figlet.py test. 你的程序应该 exit viasys.exitand print an error message:Invalid usage - Run your program with
python figlet.py -a slant. 你的程序应该 exit viasys.exitand print an error message:Invalid usage - Run your program with
python figlet.py -f invalid_font. 你的程序应该 exit viasys.exitand print an error message:Invalid usage - Run your program with
python figlet.py -f slant. TypeCS50. 你的程序应该 print the following:___________ __________ / ____/ ___// ____/ __ \ / / __ \/___ \/ / / / / /___ ___/ /___/ / /_/ / ____//____/_____/____/ - Run your program with
python figlet.py -f rectangles. TypeHello, world. 你的程序应该 print the following:_____ _ _ _ _ | | |___| | |___ _ _ _ ___ ___| |_| | | | -_| | | . |_ | | | | . | _| | . | |__|__|___|_|_|___| | |_____|___|_| |_|___| |_| - Run your program with
python figlet.py -f alphabet. TypeMoo. 你的程序应该 print the following:M M MM MM M M M ooo ooo M M o o o o M M ooo ooo
You can execute the below to check your code using check50, a program that CS50 will use to test your code when you 提交. But be sure to test it自己 as well!
check50 cs50/problems/2022/python/figlet
Green smilies 意思 your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.
如何提交
No need to 提交! This is a 练习题.