This is one full question. Please write in Python.
3. Suppose you're collecting some statistics on the students at a university, and you want to know how many students are pursuing each major. (a) (4 points) Write a program named major_counter.py that repeatedly gets input for the majors of a set of students, until the user enters X (case-insensitive) to exit. After the user input ends, show all the majors that were entered along with how many times each one appeared. Your code should use a dictionary whose keys are the majors entered and whose values are the count of each of those majors. Example program run (underlined parts indicate what the user enters) Enter major of student (x to exit): PSYC Enter major of student (X to exit): MATH Enter major of student (X to exit): COMP Enter major of student (X to exit): PSYC Enter major of student (x to exit): PSYC Enter major of student (X to exit): COMP Enter major of student (X to exit): COMP Enter major of student (X to it): CMPE Enter major of student (X to exit): CJUS Enter major of student (X to exit): PHYS Enter major of student (X to exit): COMP Enter major of student (X to exit): MATH Enter major of student (x to exit): x Summary of data entered: PSYC - 3 MATH - 2 COMP - 4 CMPE - 1 CJUS - 1 PHYS - 1
(b) (3 points) The program above works, but it's not robust to variations in the user input. For example, entering computer science, comp sci, comp, and COMP will record one count each in 4 separate majors, when it should be recording 4 students all in COMP. Above your existing program code in major_counter.py, write a function get major_code(major) that returns the appropriate major code (in capitals) based on the string parameter major (case-insensitive). Your function should work for all the variations in the following table.
Major Code COMP CMPE Variations compsci, comp sci, computer sci, computer science compeng, comp eng, computer eng, computer engineering eleceng, elec eng, elec engineering, electrical engineering mathsci, math sci, mathematical sci, mathematical sciences, mathematics EE MATH Your function should use a dictionary whose values are the major codes in the table. If the major parameter doesn't match any of the variations in the table, the function should just return a capitalized version of major. Here are some example arguments and their expected return values: Argument 'comp sci' COMPUTER Sci' 'mathemaTICS' Return Value COMP COMP MATH MATHS SLOTH STUDIES' maths' Sloth Studies' (c) (1 point) Finally, modify your original major counter.py code to use the new function. (This should require minimal changes!) Example program run (underlined parts indicate what the user enters) Enter major of student (X to exit): psychology Enter major of student (X to exit): mathematics Enter major of student (X to exit): computer SCIENCE Enter major of student (x to exit): psyc Enter major of student (x to exit): COMP SCI Enter major of student (X to exit): MatH sci Enter major of student (X to exit): Computer Engineering Enter major of student (x to exit): compsci Enter major of student (X to exit): mUSic Enter major of student (X to exit): ELECENG Enter major of student (X to exit): math
Argument Return Value comp sci' COMPUTER Sci' mathemaTICS' 'maths' Sloth Studies' COMP' COMP' MATH MATHS' SLOTH STUDIES' (c) (1 point) Finally, modify your original major counter.py code to use the new function. (This should require minimal changes!) Example program run (underlined parts indicate what the user enters) Enter major of student (X to exit): psychology Enter major of student (X to exit): mathematics Enter major of student (X to exit): computer SCIENCE Enter major of student (X to exit): psyc Enter major of student (X to exit): COMP SCI Enter major of student (X to exit): MatH sci Enter major of student (X to exit): Computer Engineering Enter major of student (X to exit): compsci Enter major of student (X to exit): mUSic Enter major of student (X to exit): ELECENG Enter major of student (X to exit): math Enter major of student (X to exit): x Summary of data entered: PSYCHOLOGY - 1 MATH - 3 COMP - 3 PSYC - 1 CMPE - 1 MUSIC - 1 EE - 1
This is one full question. Please write in Python.
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
This is one full question. Please write in Python.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!