In: Computer Science
Create a program that:
Save the program and submit it to this site for grading.
(This is Python)
Code in python
# with open("Exam1_Q3.txt", "r") as filestream:
# for line in range(i,filestream.size()):
# currentline = filestream[i].split("|")
# print(currentline)
total_sales = 0
item_name = input("Enter item name: ")
item_quantity = int(input("Enter item quantity: "))
item_price = float(input("Enter item price: "))
item_ex_price = item_quantity * item_price
total_sales+=item_ex_price
#items separated with comma and ends with a new line
data = "Item: %s,Quantity: %d,Price: $%d,Extended Price: $%d\n" % (item_name, item_quantity, item_price, item_ex_price)
print(data)
#save to file
filename = input("Enter file name: ")
f = open(filename, 'w')
f.write(data)
more_entry = input("Do you have more items to enter? y/n: ")
while more_entry=="y":
item_name = input("Enter item name: ")
item_quantity = int(input("Enter item quantity: "))
item_price = float(input("Enter item price: "))
item_ex_price = item_quantity * item_price
total_sales+=item_ex_price
data = "Item: %s,Quantity: %d,Price: $%d,Extended Price: $%d\n" % (item_name, item_quantity, item_price, item_ex_price)
f.write(data)
more_entry = input("Do you have more items to enter? y/n: ")
f.close()
print("Total sales = $%f" % (total_sales))
if(total_sales>100):
discount = 0.1*total_sales
print("Discount = $%f" % (discount))
total_sales -= discount
sales_tax = 0.08*total_sales
print("Tax = $%f" % (sales_tax))
total_sales += sales_tax
print("Total = $%f" % (total_sales))
Sample Console Input/Output
Enter item name: mango
Enter item quantity: 2
Enter item price: 20
Item: mango,Quantity: 2,Price: $20,Extended Price: $40
Enter file name: output.txt
Do you have more items to enter? y/n: y
Enter item name: orange
Enter item quantity: 4
Enter item price: 40
Do you have more items to enter? y/n: y
Enter item name: apple
Enter item quantity: 1
Enter item price: 10
Do you have more items to enter? y/n: n
Total sales = $210.000000
Discount = $21.000000
Tax = $15.120000
Total = $204.120000
Contents of output.txt after execution
Item: mango,Quantity: 2,Price: $20,Extended Price: $40
Item: orange,Quantity: 4,Price: $40,Extended Price: $160
Item: apple,Quantity: 1,Price: $10,Extended Price: $10
Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.