Reputation: 1
I'm a noob in writing python code. Here is where I'm stuck right now. The entry data input needs to be converted to the float for calculation the total. Also how can I get this data written on a new CSV file? Sum_total = amount of milk *2.99 + amount of eggs * $2.49 + amount of orange juice * $3.25 + amount of corn flakes * $4.25. Format of new csv file named receipt.csv : The first column should contain the name of each item; the second column should contain the number of each item that the customer would like to purchase; and the third column should contain subtotals for how much the customer is spending on each grocery item. After the rows for each item, your CSV should contain a “spacer” line filled with “--" in each cell. Finally, the bottom row of your CSV should be labeled “Total” and should contain the total amount that the customer needs to pay to Gerry’s groceries
import tkinter
import tkinter.messagebox
import csv
root = tkinter.Tk()
root.title("Gerry's groceries checkout")
root.configure( bg = "khaki1")
#Milk
milk_label = tkinter.Label(root, text = "Milk ($2.99):")
milk_label.grid(row = 0, column = 0)
milk_label.configure( bg = "khaki1")
milk_amount = tkinter.Entry(root, width=10)
milk_amount.grid(row = 0, column = 1)
#Eggs
eggs_label = tkinter.Label(root, text = "Eggs ($2.49):")
eggs_label.grid(row = 1, column = 0)
eggs_label.configure (bg = "khaki1")
eggs_amount = tkinter.Entry(root, width = 10)
eggs_amount.grid(row = 1, column = 1)
#Orange Juice
oj_label = tkinter.Label(root, text = "Orange juice ($3.25):")
oj_label.grid( row = 0, column = 2)
oj_label.configure( bg = "khaki1")
oj_amount = tkinter.Entry(root, width = 10)
oj_amount.grid(row = 0, column = 3)
#Corn Flakes
cf_label = tkinter.Label(root, text = "Corn flakes ($4.25)")
cf_label.grid( row = 1, column = 2)
cf_label.configure( bg = "khaki1")
cf_amount = tkinter.Entry(root, width = 10)
cf_amount.grid(row = 1, column = 3)
def checkout():
"""Checkout the total price"""
try:
total_eggs = eggs_amount * 2.49
total_oj = oj_amount *3.25
total_cf = cf_amount *4.25
total_milk = milk_amount *2.99
total = total_eggs + total_oj + total_cf + total_milk
return total
#Create a CSV file
with open("receipt.csv", "w") as file:
writer = csv.writer(file, lineterminator = "\n")
row1 = ["Milks", "Orange Juice", "Eggs", "Corn flake"]
row2 = [milk_amount,oj_amount,eggs_amount,cf_amount]
row3 = [total]
writer.writerow(row1)
writer.writerow(row2)
writer.writerow(row3)
tkinter.messagebox.showinfo("Checkout complete!", "Eceipt was written to receipt.csv.")
except:
tkinter.messagebox.showwarning("Warning!", "Sorry, the receipt could not be written. Please check that only integer values were enterered.")
#Checkout
checkout_button = tkinter.Button(root, text = " Checkout", command = checkout)
checkout_button.configure( bg = "spring green")
checkout_button.grid(row = 0, column = 4)
#Quit
quit_button = tkinter.Button(root, text = " Quit", command = root.destroy)
quit_button.configure( bg = "red2")
quit_button.grid(row = 1, column = 4)
root.mainloop()
Upvotes: 0
Views: 108
Reputation: 19375
The entry data input needs to be converted to the float for calculation the total.
Before that, you need to call .get()
for the entry data; then, you can convert the gotten string to a number:
total_eggs = int(eggs_amount.get()) * 2.49
total_oj = int( oj_amount.get()) * 3.25
total_cf = int( cf_amount.get()) * 4.25
total_milk = int(milk_amount.get()) * 2.99
Also how can I get this data written on a new CSV file?
First, you have to drop the return total
line, so that the checkout function gets to the part where you already write to the file. Then, you just have to write what your assignment states:
writer.writerow(["Milks", milk_amount.get(), total_milk])
writer.writerow(["Orange Juice", oj_amount.get(), total_oj ])
writer.writerow(["Eggs", eggs_amount.get(), total_eggs])
writer.writerow(["Corn flake", cf_amount.get(), total_cf ])
writer.writerow(["------------", "-", "-----" ])
writer.writerow(["Total", "", total ])
Upvotes: 1