Question
I need help!! Rewrite your program from Unit 5 so that it reads strings from the file and builds the list courses instead of defining
I need help!!
- Rewrite your program from Unit 5 so that it reads strings from the file and builds the list courses instead of defining all the data as part of your program itself. The file aspect should be the only thing that you change - the rest of the program logic should remain the same. Note that when reading from a file, each line contains a newline character, , which will cause your table to be double spaced. See the string method .strip() in Chapter 9.5 for an easy way of fixing this.
- Add your test cases from the program in Unit 5 at the end of the text file. You can edit the text file with Notepad, or IDLE, or Thonny or any text editor on your system.
# Defines table 1 def table1(courses): print(" Table 1 ") total = 0 for course in courses: # Loops course data data = course.split(" ") deptcode = data[0].strip() crsenum = data[1].strip() enrollees = data[len(data) - 1].strip() print(deptcode + str(crsenum), enrollees) total += int(enrollees) print(" Total: ", total)
# Defines Table 2 def table2(courses): print(" Table 2 ") for course in courses: # Loops course data data = course.split(" ") # splits the data by spaces nm = (" ".join(data[2:len(data) - 1])).strip() # starts from index 2 until len -1 new_line = "{} {} {:3}".format(data[0], data[1], nm[:23], data[len(data) - 1]) print(new_line)
# Defines Table 3 def table3(courses): print(" Table 3 ") item = {} k = 0 for course in courses: # Loops course data data = course.split(" ") nm = (" ".join(data[2:len(data) - 1])).strip() item[k] = len(nm) k += 1 sorted_x = sorted(item.items(), key=lambda kv: kv[1], reverse=True) # sorts the dictionary for i in sorted_x: data = courses[i[0]].split() new_line = "{} {} {:3}".format(data[0], data[1], ' '.join(data[2:len(data)-1]), data[-1]) print(new_line)
def main(): courses = ['CS 152 Introduction to Python Programming 21', 'CS 369 Operating Systems Administration 8', 'CS 365 Computer Networking 19', 'CS 208 Discrete Mathematics 124', 'CS 319 Computer Architecture 14', 'MA 221 Calculus and Analytical Geometry for Majors I 12', 'MA 311 Linear Algebra 7', 'MA 150 Precalculus Mathematics 27', 'CS 335 Introduction to Cybersecurity 20', 'IS 361 Data Management Systems 22', 'MG 315 Advanced Business Statistics 6']
courses.append('CS 300 Technology in a Global Society 35') for aCourse in courses: print(aCourse)
table1(courses) table2(courses) table3(courses)
main()
14 5 # Defines table 1 6 def table1(courses): 7 print(" Table 1 ") 8 total = 0 9 for course in courses: # Loops course data 19 data = course.split("") 11 deptcode = data[@].strip) 12 crsenum = data[1].strip) 13 enrollees = data[len (data) - 1].strip) print(deptcode + str(crsenum), enrollees) 15 total += int(enrollees) 16 print(" Total: ", total) 17 18 19 # Defines Table 2 20 def table2 (courses): 21 print(" Table 2 ") 22 for course in courses: # Loops course data 23 data = course.split("") # splits the data by spaces 24 nm = (" ".join(data[2:len (data) - 1])).strip) # starts from index 2 until len - 1 25 new_line = "{}{} {:3}".Format(data[@], data[1], nm[:23], data[len (data) - 1]) 26 print(new_line) 27 28 # Defines Table 3 29 def table3(courses): 30 print(" Table 3 ") 31 item = {} 32 33 for course in courses: # Loops course data 34 data = course.split(" ") 35 nm = (" ".join(data[2:len (data) - 1])).strip() 36 item[k] = len(nm) 37 | k += 1 38 sorted_x = sorted(item.items(), key=lambda kv: kv[1], reverse=True) # sorts the dictionary 39 for i in sorted_x: 40 data = courses[i[@]].split() 41 new_line = "{}{} {:3}".Format(data[@], data[1], ' '.join(data[2:len (data)-1]), data[-1]) 42 print(new_line) 43 44 45 def main(): 46 courses = ['CS 152 Introduction to Python Programming 21', 47 'CS 369 Operating Systems Administration 8', 48 'CS 365 Computer Networking 19', 49 'CS 208 Discrete Mathematics 124', 50 'CS 319 Computer Architecture 14', 51 "MA 221 Calculus and Analytical Geometry for Majors I 12', 52 MA 311 Linear Algebra 7' 53 'MA 150 Precalculus Mathematics 27', 54 'CS 335 Introduction to Cybersecurity 20', 55 'IS 361 Data Management Systems 22', 56 "MG 315 Advanced Business Statistics 6'] 57 58 59 courses.append('CS 300 Technology in a Global Society 35') 60 61 for aCourse in courses: 62 print(aCourse) 63 64 tablel(courses) Shell Python 3.7.9 (bundled) >>>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started