You are going to write a program that reads data from a file into a list and allows the user to query and modify the data. You may assume that the file is formatted properly for this assignment.
Design specifications: The program must use lists. For full credit, the program should read all data from one file (see Appendix A). For slightly less credit (-5), the program could read all data from four files (one for names, one for continents, one for populations, and one for areas) (see Appendix B). For less credit than that (-10), the program can hard-code in the three lists of data (see Appendix C). (Start with hard-coded lists, get the program working, and then work on reading in the lists from files.) For each menu option, there should be a function that takes the lists as parameters. There should be no global variables.
Sounds like fun. Hope you get an A.
Reading data from four files line by line sounds cool. This remind me to look into contextlib.
Example without modification of data:
import contextlib
import sys
def read_files_iterative(filenames):
with contextlib.ExitStack() as stack:
try:
files = [stack.enter_context(open(fname)) for fname in filenames]
except (FileNotFoundError, PermissionError) as e:
print(e, file=sys.stderr)
raise
for data in zip(*files):
yield data
def main():
if len(sys.argv) != 5:
print('4 Filenames are required')
return 1
filenames = sys.argv[1:5]
try:
for row in read_files_iterative(filenames):
print(row)
except:
return 2
# putting them into a list:
# content = list(read_files_iterative(filenames))
# print(content)
return 0
if __name__ == '__main__':
# be a nice shell citizen and return an error code
sys.exit(main())
Conclusion: This code won't help you and it does not fit to your professors requirements.