Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise File: Here are 4 things that need to be accomplished (not necessarily i

ID: 666528 • Letter: E

Question

Exercise File: Here are 4 things that need to be accomplished (not necessarily in this order) 1. Read the text from file Diamond.txt 2. Exchange all ?x?s with a character that the user selected The file Diamond.txt remains unchanged 3. Print the modified text on the screen 4. Save the modified text to a file called Diamond2.txt Important: Make sure to include appropriate exception handling The code needs to produce different output if I run it with a different text file Sample Output: Choose a character: o Modified text:

Explanation / Answer

# Opening file in read mode
file = open("Diamond.txt",'r');

# Taking the character which will replace 'x' from the file
print "choose a character : ",
ch = raw_input();

output = open("Diamond2.txt",'w')

# Replacing the required thing and write to Diamond2.txt
for line in file:
   output.write(line.replace('x',ch))

file.close();
output.close();


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote