Can you plaese do these questions for me Our users do not want to have to re-enter the price levels each time the app starts. Therefor, 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). Add the purple code to your Main Code Section (type it by hand if you get weird errors – especially the spaces - since PowerPoint might use Unicode characters that Python don’t recognize): # ************************************************************************************************* # 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 userInput = 99 while userInput != 0: checkerObj.displayList() userInput = checkerObj.displayMenu() if(userInput == 1): checkerObj.addLevel() checkerObj.writeLevelsToFile() # Write levelsList to LevelsFile elif(userInput == 2): checkerObj.removeLevel() checkerObj.writeLevelsToFile() # Write levelsList to LevelsFile elif(userInput == 3): 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 - since PowerPoint might use Unicode characters that Python don’t recognize): # Method: Load levelsList using the data in levelsFile def readLevelsFromFile(self): 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 ... # 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 - since PowerPoint might use Unicode characters that Python don’t recognize): # 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 ...
Can you plaese do these questions for me
Our users do not want to have to re-enter the price levels each time the app starts.
Therefor, 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).
Add the purple code to your Main Code Section (type it by hand if you get weird errors – especially the spaces - since PowerPoint might use Unicode characters that Python don’t recognize):
# *************************************************************************************************
# 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
userInput = 99
while userInput != 0:
checkerObj.displayList()
userInput = checkerObj.displayMenu()
if(userInput == 1):
checkerObj.addLevel()
checkerObj.writeLevelsToFile() # Write levelsList to LevelsFile
elif(userInput == 2):
checkerObj.removeLevel()
checkerObj.writeLevelsToFile() # Write levelsList to LevelsFile
elif(userInput == 3):
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 - since PowerPoint might use Unicode characters that Python don’t recognize):
# Method: Load levelsList using the data in levelsFile
def readLevelsFromFile(self):
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
...
# 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 - since PowerPoint might use Unicode characters that Python don’t recognize):
# 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
...
Step by step
Solved in 3 steps