Overview and Requirements Write a program (ascii_art_text.py) to generate files
ID: 3575670 • Letter: O
Question
Overview and Requirements
Write a program (ascii_art_text.py) to generate files containing words represented by ASCII art. I have used this website to generate 26 files each containing ASCII art for a capital letter of the alphabet (there are 26 letters in the alphabet). For this assignment, you will need to download a zip file containing these 26 files: stars.zip. (https://raw.githubusercontent.com/gsprint23/cpts111/master/progassignments/files/stars.zip ) Unzip this stars.zip into a folder called stars. Take a look at A.txt, pretty cool huh? Check out the different fonts available from the website I used, they are awesome!
Program Details
The program should perform the following steps:
Populate a list of file names for each letter text file. To do this, use the os module's function called listdir(<string path to directory>) that returns a list of the files in a particular directory.
Generate a dictionary, called letter_art, of key:value pairs where each key is a letter string and each value is a list of strings representing the lines of the corresponding letter's ASCII art. For example, the key "A" maps to the list of strings representing the lines in A.txt. Recall: <file object>.readlines()returns a list of string lines in a <file object>.
Update each ASCII art value in letter_art such that the ASCII art is composed of the same letter the ASCII art represents. For example, letter_art["A"] should store the ASCII art for A made of "A"s and "a"s. For an example, please see the output below.
Prompt the user for a word and store the user-entered string in a variable named word.
Convert word to all uppercase.
For each letter in word, write the corresponding ASCII art letter (from the letter_art dictionary) horizontally to an output file word_art.txt. Place a space line between ASCII art letters.
Starter Code
Here is some starter code to read in the ASCII art from the files. If you are running a unix-based operating system (e.g. Mac, Linux distributions), you will need to replace the back slashes with forward slashes in the file names.
Note: This starter code should be in a python source file, ascii_art_text.py, in the same folder (e.g. Bonus_PA) containing the stars folder. So the directory structure looks like the following:
Explanation / Answer
import os
def gen_file_contents(file_list: list):
letters = list()
for file in file_list:
with open("stars\" + file, "r") as file:
lines = file.read().splitlines()
letters.append(lines)
return letters
def gen_letter_dict(letter_list: list) -> dict:
letter_dict = dict()
letters = [chr(i).upper() for i in range(97, 123)]
for i in range(len(letters)):
letter_dict[letters[i]] = letter_list[i]
return letter_dict
def get_user_word() -> str:
return input("Enter a word to get converted to ASCII art. >> ").upper()
def print_ASCII_art(word: str, letter_art: dict) -> None:
values = [chr(i).upper() for i in range(97, 123)]
for letter in word:
word_val = ord(letter) # W = 87, S = 83, U = 85
for line in letter_art.get(chr(word_val)):
print(line)
def main():
file_list = os.listdir("stars")
letters = gen_file_contents(file_list) # A list of lists.
letter_art = gen_letter_dict(letters)
word = get_user_word()
print_ASCII_art(word, letter_art)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.