Using python, write code for problem. The text file USPres.txt is used and i have put screenshot of contents of the file. Please also put screenshot of your code.
Expert Answer
For this question we can solve in multiple ways, here I’m using Doctionary, because its internally uses hashing techiniques , so based on range it is simple for acces values random way.
1) I handled error handling for file oiperation
2) Handled the range mismatching
The code is as follows :
d = {}
try:
with open(“USPres.txt”) as f:
for line in f:
(key, val) = line.split()
d[int(key)] = val
except IOError:
print “Error: can’t find file or read data”
lowRange = int(raw_input(“Please enter the lower number for the range:”))
highRange = int(raw_input(“Please enter the higher number for the range:”))
for key in xrange(lowRange,highRange):
print key, d[int(key)]
As a result , Output as you expected