Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the coffee program in class lecture. Modify each of the 5 functions to make it into a module. Write a program to import all

Using the coffee program in class lecture. Modify each of the 5 functions to make it into a module. Write a program to import all five of the modules you have created. In this program you will create a menu so that the user can choose what the user wants to do. For example user can choose to add a new coffee record, delete a coffee record, modify a coffee record, search a coffee record or show coffee records. You must use the same file names for the functions that were given to you when you save a modules. For example, add_coffee_record.py, delete_coffee_record.py, modify_coffee_records.py, search_coffee_records.py, and show_coffee_records.py. Upload only the menu program you have created.

Notes: Files

Open text files using the open, read/write, close

outfile = open(test.txt, w) outfile.write(Score) outfile.close()

Opens a text using the with statement. Using the with statement will automatically closes the file

With open(test.txt,w) as outfile: outfile.write(Score) #need to indent

More List stuff

List Methods for modifying a list

append(item)

insert(index, item)

remove(item)

append item to end of list. insert item into specified index

removes first item in the list that is equal to the specified item.

ValueError raised if not found

index(item) raised if not found.

pop(index) the list and

The pop() method

returns the index of the first occurrence of specified item. ValueError

if no index argument is specified, this method gets the last item from

removes it. Otherwise, this method gets the item at the specified index and removes it.

inventory = ["staff", "hat", "robe", "bread"] item = inventory.pop() # item = "bread" since no arguments remove from the end of list

# inventory = ["staff", "hat", "robe"] item = inventory.pop(1) # item = "hat" remove at index 1

# inventory = ["staff", "robe"] The index() and pop() methods

inventory = ["staff", "hat", "robe", "bread"] i = inventory.index("hat") # 1 inventory.pop(i) # ["staff", "robe", "bread"]

try and except for trapping errors.

The hierarchy for five common errors

Exception OSError

FileExistsError

FileNotFoundError ValueError

How to handle a ValueError exception

try: number = int(input("Enter an integer: ")) print("You entered a valid integer of " + str(number) + ".")

except ValueError: print("You entered an invalid integer. Please try again.")

print("Thanks!")

output with error:

Enter an integer: five You entered an invalid integer. Please try again. Thanks!

Code that handles multiple exceptions (example used with files)

filename = input("Enter filename: ") movies = [] try:

with open(filename) as file: for line in file: line = line.replace(" ", "") movies.append(line)

except FileNotFoundError: print("Could not find the file named " + filename)

except OSError: print("File found - error reading file")

except Exception: print("An unexpected error occurred")

Here is the coffee program examples:

# This program adds coffee inventory records to 
# the coffee.txt file. 

def add_coffee(): # Create a variable to control the loop. another = 'y'

 # Open the coffee.txt file in append mode. 

coffee_file = open('coffee.txt', 'a')

# Add records to the file. 

while another == 'y' or another == 'Y':

# Get the coffee record data. 

print('Enter the following coffee data:') descr = input('Description: ') qty = int(input('Quantity (in pounds): '))

# Append the data to the file. 

coffee_file.write(descr + ' ') coffee_file.write(str(qty) + ' ')

# Determine whether the user wants to add 
# another record to the file. 

print('Do you want to add another record?')

another = input('Y = yes, anything else = no: ')

# Close the file. 

coffee_file.close() print('Data appended to coffee.txt.')

# This program allows the user to delete 
# a record in the coffee.txt file. 

import os # Needed for the remove and rename functions

def del_coffee(): # Create a bool variable to use as a flag. found = False

# Get the coffee to delete. 

search = input('Which coffee do you want to delete? ')

# Open the original coffee.txt file. 

coffee_file = open('coffee.txt', 'r')

# Open the temporary file. 

temp_file = open('temp.txt', 'w') # Read the first record's description field.

descr = coffee_file.readline()

# Read the rest of the file. 

while descr != '':

# Read the quantity field. 

qty = float(coffee_file.readline()) # Strip the from the description.

descr = descr.rstrip(' ')

# If this is not the record to delete, then # write it to the temporary file. 

if descr != search: # Write the record to the temp file. temp_file.write(descr + ' ') temp_file.write(str(qty) + ' ')

else: # Set the found flag to True.

found = True

# Read the next description. 

descr = coffee_file.readline() # Close the coffee file and the temporary file.

coffee_file.close()

temp_file.close() # Delete the original coffee.txt file.

os.remove('coffee.txt')

# Rename the temporary file. 

os.rename('temp.txt', 'coffee.txt') # If the search value was not found in the file

# display a message. 

if found: print('The file has been updated.')

else: print('That item was not found in the file.')

# This program allows the user to modify the quantity 
# in a record in the coffee.txt file. 

import os # Needed for the remove and rename functions

def mod_coffee(): # Create a bool variable to use as a flag. found = False

 # Get the search value and the new quantity. 

search = input('Enter a description to search for: ') new_qty = int(input('Enter the new quantity: '))

# Open the original coffee.txt file. 

coffee_file = open('coffee.txt', 'r')

# Open the temporary file. 

temp_file = open('temp.txt', 'w') # Read the first record's description field.

descr = coffee_file.readline()

# Read the rest of the file. 

while descr != '':

# Read the quantity field. 

qty = float(coffee_file.readline()) # Strip the from the description.

descr = descr.rstrip(' ')

# Write either this record to the temporary file, # or the new record if this is the one that is 
# to be modified. 

if descr == search:

# Write the modified record to the temp file. 

temp_file.write(descr + ' ') temp_file.write(str(new_qty) + ' ')

# Set the found flag to True. 

found = True else:

# Write the original record to the temp file. 

temp_file.write(descr + ' ') temp_file.write(str(qty) + ' ')

# Read the next description. 

descr = coffee_file.readline() # Close the coffee file and the temporary file.

coffee_file.close()

temp_file.close() # Delete the original coffee.txt file.

os.remove('coffee.txt')

# Rename the temporary file. 

os.rename('temp.txt', 'coffee.txt') # If the search value was not found in the file

# display a message. 

if found: print('The file has been updated.')

else: print('That item was not found in the file.')

# This program allows the user to search the # coffee.txt file for records matching a # description. 

def search(): # Create a bool variable to use as a flag. found = False

# Get the search value. 

search = input('Enter a description to search for: ')

# Open the coffee.txt file. 

coffee_file = open('coffee.txt', 'r') # Read the first record's description field.

descr = coffee_file.readline()

# Read the rest of the file. 

while descr != '':

# Read the quantity field. 

qty = float(coffee_file.readline()) # Strip the from the description.

descr = descr.rstrip(' ') # Determine whether this record matches

# the search value. 

if descr == search:

# Display the record. 

print('Description:', descr)

print('Quantity:', qty)

print() # Set the found flag to True.

found = True

# Read the next description. 

descr = coffee_file.readline()

# Close the file. 

coffee_file.close() # If the search value was not found in the file

# display a message. 

if not found: print('That item was not found in the file.')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

Was there an effort to involve the appropriate people?

Answered: 1 week ago

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago