I am looking for PYTHON code that will display the following:
Code a menu-driven application that does the following:
The user can enter data for a product name, product code, and unit price.
An example might be 'Breaburn Apples', 'BAP'. 1.99. After entering the data, it is appended (note I said 'appended') as a single line of text to a text file. Each line of text in the file should represent a single product.
There is an option to display the contents of the text file.
The program runs until the user tells it to stop.
Here's a possible run of your program.
Inventory program
1) Enter a product
2) Display all products
3) Quit
Select a menu option: 1
Enter a product
Name: Braeburn Apples
Code: BAP
Price: 1.99
Inventory program
1) Enter a product
2) Display all products
3) Quit
Select a menu option: 1
Enter a product
Name: Joliet Pears
Code: JPR
Price: 3.09
Inventory program
1) Enter a product
2) Display all products
3) Quit
Select a menu option: 2
Products on file...
Braeburn Apples, BAP, 1.99
Joliet Pears, JPR, 3.09
Inventory program
1) Enter a product
2) Display all products
3) Quit
Select a menu option: 3
The program has ended.
If your text file doesn't exist and you attempt to display it, your
code will fail. At this time, don't worry about that. However, if
you want to code for that potential problem, feel free. You never
know when that might be useful.
Please find the below python code with comments:
#importing required pacakges
import os.path
from os import path
option=1
#checking option
while option!=3 :
print('Inventory program\n1) Enter a product\n2)
Display all products\n3) Quit\nSelect a menu option:',end='')
#reading choice
option=int(input())
if option==1 :
#we are opening file in append mode
so if there is no file it will be created
f=open("products.txt", "a+")
print('Enter a product')
#reading product name
Name=input('Name:')
#reading product code
Code=input('Code:')
#reading product price
Price=input('Price:')
#appending to file with new
line
f.write(Name+','+Code+','+Price+'\n')
f.close()
elif option==2 :
#checking if the file already
exists for reading
if path.exists("products.txt")
:
f=open("products.txt", "r")
#reading
products
productslist=f.read()
#displaying
products
print(productslist)
f.close()
else :
print('No such
file exists')
elif option==3 :
exit()
else :
print('Invalid option')
Get Answers For Free
Most questions answered within 1 hours.