Python 3.6 Bachelor Degrees Table 5.8 shows the number of bachelor degrees confe
ID: 3850664 • Letter: P
Question
Python 3.6
Bachelor Degrees Table 5.8 shows the number of bachelor degrees conferred in 1981 and 2010 in certain fields of study. Tables 5.9 and 5.10 show the percentage change and a histogram of 2010 levels, respectively. Write a program that allows the user to display any one of these tables as an option and to quit as a fourth option. Table 5.8 is ordered alphabetically by field of study, Table 5.9 is ordered by decreasing percentages, and Table 5 is ordered by increasing number of degrees. Use the file DegreesDict.dat that stores a dictionary where each field of study is a key and each value is a two-tuple of the form (number of degrees in 1981, number of degrees in 2010). One item of the dictionary is "Business" (200521, 358293).Explanation / Answer
Code:
#!/usr/local/bin/python3
def main():
with open("DegreesDict.dat") as fp:
degrees = {}
for each_line in fp:
if 'Field of Study' in each_line:
continue
# fetching data from file into variables
field, deg_in_1981, deg_in_2010 = each_line.strip().split(',')
change = ((float(deg_in_2010) - float(deg_in_1981)) / float(deg_in_1981)) * 100
# constructing a nested dictionary from the values in file
degrees[field] = {}
degrees[field]['1981'] = int(deg_in_1981)
degrees[field]['2010'] = int(deg_in_2010)
degrees[field]['change'] = change
fp.close()
while True:
# displaying the menu
print(" Bachelor Degrees Menu")
print("---------------------")
print("1. Bachelor Degrees conferred in certain fields")
print("2. Percentage change in bachelor degrees conferred")
print("3. Bachelor Deegrees conferred in 2010 in certain fields")
print("4. Exit from Menu")
# taking option from the user
option = int(input('Enter your option(1/2/3/4)? '))
if option == 1:
# sorting by course name
study = sorted(degrees.items())
print(" Bachelor Degree conferred in certain fields")
print("--------------------------------------------------------------------")
print("{0:<30s} {1:<30s} {2:<30s}".format('Field of Study', '1981', '2010'))
print("--------------------------------------------------------------------")
for key, value in study:
d_1981 = value['1981']
d_2010 = value['2010']
print("{0:<30s} {1:<30s} {2:<30s}".format(str(key), str(d_1981), str(d_2010)))
print("--------------------------------------------------------------------")
elif option == 2:
per = {}
for field in degrees:
per[field] = degrees[field]['change']
# sorting by percentage which is second field i.e. index 1 because array starts from 0
per = sorted(per.items(), key=lambda x:x[1], reverse=True)
print(" Perchange change in bachelor degrees conferred")
print("-----------------------------------------------------------------")
print("{0:<30s} {1:<30s}".format('Field of Study', '% Change(1981-2010)'))
print("-----------------------------------------------------------------")
for key, value in per:
print("{0:<30s} {1:<30.1f}".format(str(key), value))
elif option == 3:
deg_2010 = {}
for field in degrees:
deg_2010[field] = degrees[field]['2010']
# sorting by second field which is number of degrees in a particular course
deg_2010 = sorted(deg_2010.items(), key=lambda x:x[1])
print(" Bachelor degrees conferred in 2010 in certain fields")
print("------------------------------------------------------")
padcnt = 3
for key, value in deg_2010:
# adding *** padding so that output shows in required format
padding = '***' * padcnt
print("{0:<30s} {2:<10s} {1:<20s}".format(str(key), str(value), str(padding)))
padcnt += 3
elif option == 4:
break
else:
print("Invalid option. Please try again")
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> cat DegreesDict.dat
Field of Study,1981,2010
Business,200521,358293
Computer and Info Science,15121,39589
Education,108074,101265
Engineering,63642,72654
Social sciences and history,100513,172780
Unix Terminal> python bt.py
Bachelor Degrees Menu
---------------------
1. Bachelor Degrees conferred in certain fields
2. Percentage change in bachelor degrees conferred
3. Bachelor Deegrees conferred in 2010 in certain fields
4. Exit from Menu
Enter your option(1/2/3/4)? 1
Bachelor Degree conferred in certain fields
--------------------------------------------------------------------
Field of Study 1981 2010
--------------------------------------------------------------------
Business 200521 358293
Computer and Info Science 15121 39589
Education 108074 101265
Engineering 63642 72654
Social sciences and history 100513 172780
--------------------------------------------------------------------
Bachelor Degrees Menu
---------------------
1. Bachelor Degrees conferred in certain fields
2. Percentage change in bachelor degrees conferred
3. Bachelor Deegrees conferred in 2010 in certain fields
4. Exit from Menu
Enter your option(1/2/3/4)? 2
Perchange change in bachelor degrees conferred
-----------------------------------------------------------------
Field of Study % Change(1981-2010)
-----------------------------------------------------------------
Computer and Info Science 161.8
Business 78.7
Social sciences and history 71.9
Engineering 14.2
Education -6.3
Bachelor Degrees Menu
---------------------
1. Bachelor Degrees conferred in certain fields
2. Percentage change in bachelor degrees conferred
3. Bachelor Deegrees conferred in 2010 in certain fields
4. Exit from Menu
Enter your option(1/2/3/4)? 3
Bachelor degrees conferred in 2010 in certain fields
------------------------------------------------------
Computer and Info Science ********* 39589
Engineering ****************** 72654
Education *************************** 101265
Social sciences and history ************************************ 172780
Business ********************************************* 358293
Bachelor Degrees Menu
---------------------
1. Bachelor Degrees conferred in certain fields
2. Percentage change in bachelor degrees conferred
3. Bachelor Deegrees conferred in 2010 in certain fields
4. Exit from Menu
Enter your option(1/2/3/4)? 4
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.