PYTHON: A file of student records contains name, sex (M or F), age (in years) an
ID: 3631967 • Letter: P
Question
PYTHON:A file of student records contains name, sex (M or F), age (in years) and marital status (single or married) for each student. Design and algorithm that will read through the file and calculate the numbers of married men, single men, married women and single women. Print these numbers on a student summary report. If any single men are over 30 years of age, print their names and ages on a separate eligible bachelors report
Explanation / Answer
Set marriedMen to 0 Set singleMen to 0 Set marriedWomen to 0 Set singleWomen to 0 Set hasPrintedHeader to False READ name, sex, age, status DOWHILE(NOT EOF) IF (status = married) THEN //check if status is married, if yes then check next IF (sex = 'F') THEN //check if sex is F, if yes then +1 marriedWomen = marriedWomen + 1 ELSE IF (sex = 'M') THEN //under married, and sex is M then +1 marriedMen = marriedMen + 1 ENDIF ENDIF ENDIF IF (status = single) THEN //check if status is single, if yes then check next IF (sex = 'F') THEN //check if sex is F, if yes then +1 to singleWomen singleWomen = singleWomen + 1 ELSE IF (sex = 'M') THEN //under single, and sex is M then +1 singleMen = singleMen + 1 IF (age > 30) THEN //under single, sex = M and age is over 30 then print the name, age IF (hasPrintedHeader = False) THEN Print 'Eligible bachelors Report' hasPrintedHeader = True END IF Print 'Name: ', name Print 'Age: ', age ENDIF ENDIF ENDIF ENDIF READ next record ENDDO Print 'Student Summary Report' Print 'Married Men: ', marriedMen Print 'Single Men: ', singleMen Print 'Married Women: ', marriedWomen Print 'Single Women: ', singleWomen
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.