Question: PYTHON CODING QUESTION Write a program with two functions: write_file and determine_major . The write_file function will accept a file name and write the following
PYTHON CODING QUESTION
Write a program with two functions: write_file and determine_major. The write_file function will accept a file name and write the following data to that file.
INSY
350
MANA
256
FINA
120
ACCT
184
MARK
86
OPMA
50
The determine_major function will accept a file name and a major and return the major and the number of students in that major.
[Sample input/output:]
What major: INSY
Major INSY has 350 majors
What major: MARK
Major MARK has 86 majors
I've been working at this code almost all night and this is what I came up with:
def write_file(filename): outfile = open('filename', 'w') outfile.write("INSY" + ' ') outfile.write("350" + ' ') outfile.write("MANA" + ' ') outfile.write("256" + ' ') outfile.write("FINA" + ' ') outfile.write("120" + ' ') outfile.write("ACCT" + ' ') outfile.write("184" + ' ') outfile.write("MARK" + ' ') outfile.write("86" + ' ') outfile.write("OPMA" + ' ') outfile.write("50" + ' ')
outfile.close()
def determine_major(filename): #Camille sad to create a loop with an "if" statement inside it msg = '' infile = open('filename', 'r') name = infile.readline().rstrip() while name != '': stud_numb = int(infile.readline().rstrip()) msg += 'Major ' + name + ' has ' + str(stud_numb) + ' majors ' + ' ' name = infile.readline().rstrip() return msg
def main(): filename = input('What major: ') write_file(filename) print(determine_major(filename))
main()
The problem with the code is that it's printing the entire list instead of it's supposed name along with the number attached. Please help, I've been working almost all night on this and I'm not sure what I am missing.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
