Our users do not want to have to re-enter the price levels each time the app starts. Therefore, we need to save the price levels to a file on disk – called levelsFile - and populate the levelsList with the file’s items when the app starts. Also, every time the user manipulates the values in levelsList, levelsFile needs to be updated accordingly. The easiest way is not to update the file, but to simply override it (create a new file) with the new values in levelsList (each time the user manipulates items in the list). Two new methods need to be created in the PriceChecker class. They are readLevelsFromFile() and writeLevelsToFile(). And then the Main Code Section must also be updated to use these methods in the object. Note: The changes required to the Main Code Section are supplied on the next slide, but you need to complete the skeleton code of the two newly mentioned methods (supplied on subsequent slides). (code to work on)instructions in pictures
Our users do not want to have to re-enter the price levels each time the app starts. Therefore, we need to save the price levels to a file on disk – called levelsFile - and populate the levelsList with the file’s items when the app starts.
Also, every time the user manipulates the values in levelsList, levelsFile needs to be updated accordingly. The easiest way is not to update the file, but to simply override it (create a new file) with the new values in levelsList (each time the user manipulates items in the list).
Two new methods need to be created in the PriceChecker class. They are readLevelsFromFile() and writeLevelsToFile(). And then the Main Code Section must also be updated to use these methods in the object.
Note: The changes required to the Main Code Section are supplied on the next slide, but you need to complete the skeleton code of the two newly mentioned methods (supplied on subsequent slides).
(code to work on)instructions in pictures
![try:
# Set levelsList to an empty list
# Open the file
# Use a loop to read through the file line by line
# If the last two characters in the line is "\n", remove them
# Append the line to levelsList
ww w
# Close the file
except:
return
Add the following method to your PriceChecker class and replace the ellipsis with the applicable code
(type it by hand if you get weird errors - especially the spaces
# Method: Write levelsList to levelsFile (override the existing file)
def writelevelsToFile(self):
# Open the file in a way that will override the existing file (if it already exists)
...
# Use a loop to iterate over levelsList item by item
# Convert everything in the item to a string and then add \n to it - before writing it to the file
# Close the file](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F924e4664-9ae2-4745-951d-a9d3dd0dcbd4%2F1701a9d6-a979-4ffa-8ed8-7ce208136c77%2Fj1p8o3u_processed.jpeg&w=3840&q=75)
![Add the underlined code to your Main Code Section (type it by hand if you get weird errors- especially
the
Main Code Section
# Create an object based on the PriceChecker class
checkerObj = PriceChecker()
# Load levelsList from the records in levelsFile
checkerObj.readLevelsFromFile()
# Display the levelsList and get user input for what actions to take
userlnput = 99
while userinput = 0:
checkerobj.displaylist()
userinput = checkerObj.displayMenu()
if(userInput == 1):
checkerObj.addLevel()
checkerObj.writelevelsToFile() # Write levelsList to LevelsFile
elif(userinput == 2):
%3D3D
checkerObj.removelevel()
checkerObj.writelevelsToFile() # Write levelsList to LevelsFile
elif(userinput == 3):
%3D
checkerObj.removeAllLevels()
checkerObj.writelevelsToFile() # Write levelsList to LevelsFile
Add the following method to your PriceChecker class and replace the ellipsis with the applicable code
(type it by hand if you get weird errors - especially the spaces
# Method: Load levelsList using the data in levelsFile
wwwww w
def readlevelsFromFile(self):](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F924e4664-9ae2-4745-951d-a9d3dd0dcbd4%2F1701a9d6-a979-4ffa-8ed8-7ce208136c77%2Fv6p303m_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)