Question: I have a Python assignment, here are the requirements: Ask the user for the name of a file to open and read from. This file
I have a Python assignment, here are the requirements:
- Ask the user for the name of a file to open and read from. This file should be an encoded ascii art file (provided)
- Open the file and read the data.
- Because it is encoded you will need to loop through every character in the data and un-encode (decode) it
- You can do this with a command similar to the line below. Basically this is very similar to examples in the book and it just subtracts 1 from each characters value. Example makes 'B' into 'A'
-
chr(ord(character) - 1)
-
- as you loop and transform each character you will need to add them to a new string. When you are done this new string should have each and every transformed character from the old string
- You can do this with a command similar to the line below. Basically this is very similar to examples in the book and it just subtracts 1 from each characters value. Example makes 'B' into 'A'
- Print the new string you created. It should be a cute ascii art image
- Ask the user if they want to save the ascii art (Y/N)
- if they indicate Yes (Y) then ask them for a filename
- open the file for writing
- Save the new string you created to the file
- close the output file you just wrote to
- close the input file you read from
I'm have trouble with step 3 and 4. How can I loop the characters to a new string to print?
Here is what I have so far:
#open and read the file filename = input("Enter a filename:") artFile = open(filename, 'r') art = artFile.read()
picture = "" for letter in art: picture += chr(ord(letter) -1) print(art)
#saveArt = input("Do you want to save your artwork?") #if yes in saveArt:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
