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

Problem 2 [10 points]: You are given Problem2.py and USPres.txt, complete the Pr

ID: 3730146 • Letter: P

Question

Problem 2 [10 points]: You are given Problem2.py and USPres.txt, complete the Problem2.py and make sure to use USPres.txt to solve this problem. The file USPres.txt contains the names of the first 44 U.S. presidents in the order they served. Complete Problem2.py (without using a list) that requests a range of numbers and displays all presidents whose number is in that range to an output file named as "Output.txt". Sample run shows the range between 10 and 14 and the output is displayed to Output.txt file: Below is the prompt your program should display for the user: (Reminder: Range in this case is 10-14) /Library/Frameworks/Python.framework/Versions/3.6/ Enter the upper number for the range: 14 Process finished with exit code Enter the lower number for the range: 10 ii Below is what should be written to a file: ho John Tyler 11 James Polk 12 Zachary Taylor 13 Millard Fillmore 14 Franklin Pierce 1 4 5

Explanation / Answer

def main(): names = [] with open('USPres.txt', 'r') as f: for line in f: names.append(line.strip()) lower = int(input('Enter the lower number for the range: ')) upper = int(input('Enter the upper number for the range: ')) with open('Output.txt', 'w') as f: for i in range(lower, upper+1): f.write(str(i) + ' ' + names[i-1]) main()

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