How can I delete a row from .csv file - python

I am a new learner in python trying to understand the logic and the solution of this problem.Exercise says to create an order app for a coffee shop .
import uuid # GET A RANDOM ID FOR THE CUSTOMER
from datetime import date # GET CURRENT DATE
from csv import DictWriter
inlist = -1
length = 0
Total_Amount = 0.0
CustomerList = []
AddressList = []
Today_Key = date.toordinal(date.today())
Today_Date = date.today()
Print_Today = Today_Date
Customers = {}
Dates = {}
FirstEmployeeAccountUsername = "coffee1"
FirstEmployeeAccountPassword = "coffeeshop1"
SecondEmployeeAccountUsername = "coffee2"
SecondEmployeeAccountPassword = "coffeeshop2"
ThirdEmployeeAccountUsername = "coffee3"
ThirdEmployeeAccountPassword = "coffeeshop3"
print("Welcome to our coffee shop!")
print("Login")
# EMPLOYEE LOGIN PROCESS STARTS
LoginEnter = True
while LoginEnter:
username = input("Username: ")
password = input("Password: ")
if username == FirstEmployeeAccountUsername and password == FirstEmployeeAccountPassword or username == SecondEmployeeAccountUsername and password == SecondEmployeeAccountPassword or username == ThirdEmployeeAccountUsername and password == ThirdEmployeeAccountPassword:
print("Login Successful")
LoginEnter = False
else:
print("Invalid Login. Try again")
# EMPLOYEE LOGIN PROCESS ENDS
# PROCESS AFTER ORDER PLACEMENT STARTS
process1 = True
process2 = True
while process1:
while process2:
Customer_Name = input("Customer's Name:")
CustomerList.append(Customer_Name)
Customers_Address = input("Customer's Address:")
AddressList.append(Customers_Address)
if Today_Key not in Dates:
Dates[Today_Key] = {}
if Customer_Name not in Dates[Today_Key]:
Dates[Today_Key][Customer_Name] = 1
else:
Dates[Today_Key][Customer_Name] += 1
if Customer_Name in Customers:
Customers[Customer_Name]['Orders'] += 1
Customers[Customer_Name]['TotalAmount'] = Total_Amount
else:
Customers[Customer_Name] = {}
Customers[Customer_Name]['Address'] = Customers_Address
Customers[Customer_Name]['ID'] = uuid.uuid1()
Customers[Customer_Name]['Orders'] = 1
Customers[Customer_Name]['TotalAmount'] = 0
print(Customer_Name, "has ordered {} time(s)".format(Customers[Customer_Name]['Orders']))
if Customers[Customer_Name]['TotalAmount'] == 0:
print("This is the first time", Customer_Name, "orders")
else:
print(Customer_Name, "has spent", Customers[Customer_Name]['TotalAmount'], "in total")
print("Current Date is: {}".format(Today_Date))
Order_Price = float(input("Total amount of order:"))
Total_Amount = Order_Price + Total_Amount
if Print_Today != Today_Date:
print("Total amount of orders today is: ", float(Total_Amount))
answer1 = input("Send another order? (Y/N)").lower()
if answer1 == "y":
process2 = True
else:
process2 = False
LengthCustomersList = len(CustomerList)
length += 1
inlist += 1
file = open('CustomerNames.txt', 'w')
file.write(str(CustomerList[0:]) + '\n') # TAKE CARE FOR DUPLICATE NAMES FROM SAME ADDRESS
file.close()
file1 = open('Orders_Per_Users.txt', 'a')
file1.write(Customer_Name + " has ordered " + str(
Customers[Customer_Name]['Orders']) + " times in total\n") # FIX DUPLICATES SAME NAME SAME ADDRESS
file1.close()
with open('data_entered.csv', 'a') as f:
csv_writer = DictWriter(f, fieldnames=['Customer Name', 'Customer Address', 'Customer ID', 'Total Orders',
'Total Amount'])
csv_writer.writeheader()
csv_writer.writerows([{'Customer Name': CustomerList[inlist], 'Customer Address': AddressList[inlist],
'Customer ID': Customers[Customer_Name]['ID'],
'Total Orders': Customers[Customer_Name]['Orders'],
'Total Amount': Customers[Customer_Name]['TotalAmount']}])
if int(length) == int(LengthCustomersList):
process1 = False
My idea is to do something like an if statement so when the same Customer ID and the same CustomerName show up in the .csv file , one of them gets deleted, so the file does not contain any duplicates like those in the screenshot above.

I am not sure we are going to eventually solve your question but I wanted to give you some inputs that you can use to fix your code.
Use of variable Today_Date and Print_Today in your code
Today_Date = date.today()
Print_Today = Today_Date
if Print_Today != Today_Date:
These two lines are set to the same value. Later on in the code, you are checking if the are not equal. I checked Print_Today and Today_Date for reassignment. None occurs. So how do you expect these two variables to have different values?
If this program runs for infinite number of days, it will still NOT change the value of these two variables. The reason is they were defined at the beginning of the program and never changed. You may want to look into it.
The use of Today_Key in Dates dictionary in your code.
Today_Key = date.toordinal(date.today())
Dates = {}
You are using Today_Key to count the number of times a customer name was entered. I don't see a point in having Today_Key as the key unless you plan to have more than one key in the dictionary. This was set at the beginning of the program and never changed. So what do you intend to do with this key? I don't think you should have that as key. Instead you should just keep track of the customer names. Also, you are not printing or writing this information into a file. Are you intending to use this later in the program? I dont see the value and it may just be using up memory space and processing time.
Use of multiple names for Username & Password.
You have created 6 variables to store username & password. In other places, you are using dictionary. So why are you not taking advantage of the dictionary here?
FirstEmployeeAccountUsername = "coffee1"
FirstEmployeeAccountPassword = "coffeeshop1"
SecondEmployeeAccountUsername = "coffee2"
SecondEmployeeAccountPassword = "coffeeshop2"
ThirdEmployeeAccountUsername = "coffee3"
ThirdEmployeeAccountPassword = "coffeeshop3"
Instead of these, can't you just define a dict variable and check for the value as shown below?
UserLogin = {"coffee1":"coffeeshop1", "coffee2": "coffeeshop2", "coffee3": "coffeeshop3"}
username = password = ''
while True:
username = input("Username: ")
password = input("Password: ")
if (username in UserLogin) and (UserLogin[username] == password):
print("Login Successful")
break
else:
print("Invalid Login. Try again")
For your customer name counter portion, try this. I dont think you are actually doing anything with the counter. If you are, this code is much simpler.
CustomerCounts = defaultdict(int)
while True:
Customer_Name = input("Customer's Name:")
CustomerList.append(Customer_Name)
Customers_Address = input("Customer's Address:")
AddressList.append(Customers_Address)
CustomerCounts[Customer_Name] += 1
similarly, try using defaultdict and reduce a lot of code you have written. In the end, there is lot of code optimization and logic corrections you can do. However, it does not solve the infinite loop situation.

Related

Print a receipt in Python that doesn't repeat the items

I need to create a function that creates a session that accepts a "clerk's" input data about a customer's orders until the "clerk" enters the string "/". Each line of input consists of two elements: the product code and the quantity. Lines of input are formatted as follows: "{product_code},{quantity}". The function should write a file called receipt.txt that prints a summarized report of the session.
The receipt should provide a summary of all the orders made during the session and the product must only appear once if it has been ordered at least once during the session, even if it has been ordered multiple times. In other words, if a product is ordered multiple times, then it should only have one entry in the receipt that describes the sum of all of the orders made for that product. The products must appear in alphabetical order.
Here is my code right now and it prints a receipt but I don't know how to make the order appear only once and make it alphabetical order. Please help.
EDIT: Added get_property function.
def get_property(code,property):
return products[code][property]
def main():
products = {
"americano":{"name":"Americano","price":150.00},
"brewedcoffee":{"name":"Brewed Coffee","price":110.00},
"cappuccino":{"name":"Cappuccino","price":170.00},
"dalgona":{"name":"Dalgona","price":170.00},
"espresso":{"name":"Espresso","price":140.00},
"frappuccino":{"name":"Frappuccino","price":170.00},
}
orders_list = []
total = 0
while(True):
customer_order = input("Welcome to the CoffeePython POS Terminal.\nPlease enter the Product Code and the Quantity in this format - {Product Code},{Quantity}.\nEnter '/' to quit.\n")
if customer_order == "/":
break
else:
code_quantity_list = customer_order.split(",")
code = code_quantity_list[0]
quantity = code_quantity_list[1]
quantity_int = int(quantity)
if code in products:
subtotal = get_property(code,"price")*quantity_int
total += subtotal
ordered_item = dict([
('code', code),
('qty', quantity_int),
('subtotal', subtotal)
])
orders_list.append(ordered_item)
else:
print("The Product Code that you entered is invalid. Please try again.")
print("==")
print("CODE\t\t\tNAME\t\t\tQUANTITY\t\t\tSUBTOTAL")
for order in orders_list:
order_code = order['code']
order_name = products[order_code]["name"]
order_qty = order['qty']
order_subtotal = order['subtotal']
print(f"{order_code}\t\t{order_name}\t\t{order_qty}\t\t\t\t{order_subtotal}\t\t")
print(f"\nTotal:\t\t\t\t\t\t\t\t\t\t{total}")
print("==")
print("Thank you for ordering. Goodbye.")
main()
Output
==
CODE NAME QUANTITY SUBTOTAL
americano Americano 2 300.0
americano Americano 2 300.0
Total: 600.0
==
To store the orders, I would suggest you to use a dictionary with code as a key, and the price as value.
orders_list = {}
while ...:
orders_list[code] = orders_list.setdefault(code, 0) + subtotal
for product in sorted(orders_list):
subtotal = orders_list[product]
print(f"{product:<10} {subtotal}")
You need to check the saved list in orders_list and then evaluate the existing key. For sort list in the order_list by key in a dict, you can use this reference
How do I sort a list of dictionaries by a value of the dictionary?
I am adding a new method to perform check.
Also, I am not sure about your get_property() method, I changed it a bit.
def check_code(orders_list, code):
codes = []
for i in orders_list:
codes.append(i["code"])
if(code in codes):
return True, codes.index(code)
else:
return False, 0
def main():
products = {
"americano":{"name":"Americano","price":150.00},
"brewedcoffee":{"name":"Brewed Coffee","price":110.00},
"cappuccino":{"name":"Cappuccino","price":170.00},
"dalgona":{"name":"Dalgona","price":170.00},
"espresso":{"name":"Espresso","price":140.00},
"frappuccino":{"name":"Frappuccino","price":170.00},
}
orders_list = []
total = 0
while(True):
customer_order = input("Welcome to the CoffeePython POS Terminal.\nPlease enter the Product Code and the Quantity in this format - {Product Code},{Quantity}.\nEnter '/' to quit.\n")
if customer_order == "/":
break
else:
code_quantity_list = customer_order.split(",")
code = code_quantity_list[0]
quantity = code_quantity_list[1]
quantity_int = int(quantity)
if code in products:
# subtotal = get_property(code,"price")*quantity_int
subtotal = products[code]["price"] *quantity_int
check = check_code(orders_list, code)
if check[0]:
orders_list[check[1]]["subtotal"] += subtotal
orders_list[check[1]]["qty"] += quantity_int
else:
ordered_item = dict([
('code', code),
('qty', quantity_int),
('subtotal', subtotal)
])
orders_list.append(ordered_item)
total += subtotal
else:
print("The Product Code that you entered is invalid. Please try again.")
print("==")
print("CODE\t\t\tNAME\t\t\tQUANTITY\t\t\tSUBTOTAL")
orders_list = sorted(orders_list, key=lambda k: k['code'])
for order in orders_list:
order_code = order['code']
order_name = products[order_code]["name"]
order_qty = order['qty']
order_subtotal = order['subtotal']
print(f"{order_code}\t\t{order_name}\t\t{order_qty}\t\t\t\t{order_subtotal}\t\t")
print(f"\nTotal:\t\t\t\t\t\t\t\t\t\t{total}")
print("==")
print("Thank you for ordering. Goodbye.")
main()
When i test the code the item prints once and i did following:
frappuccino, 1
americano, 2
dalgona, 1
So i cannot reproduce the issue but i dont have the get_properties method either so maybe thats the issue.
As for printing the list in alphabetical order you should look to sort the list before looping and printing the reciept. You will find how you can achieve sort on a list containing dictionaries
here

How to compare filename with a string in Python

I'm making a program in which you can store your data and it will have a unique code from which you can load that back. So I've made way to make a unique code by just putting the date and then putting the entry number. For example: The date is 24 April 2021 and it is the first entry of the day then it'll generate the code as 202104241. The "1" at the end means entry number 1. So i want that to change every time you make a new entry in that day. I tried it doing like this but it gives an error. Is there any to do it?
def New_Document():
LUD = 1 # LAST UNIQUE DIGIT
unique = []
date = datetime.date.today()
unique.append(date)
unique_id = str(unique[0])
unique_id = unique_id.replace('-', '')
unique_id = unique_id + str(LUD)
folder = r'E:\\Programming related\\DATASAVER'
destination = r'E:\\Programming related\\DATASAVER\\DATA'
filenames = os.listdir(destination)
print(filenames)
for file in filenames:
if filenames[file] in unique_id:
LUD += 1
unique_id = unique_id + str(LUD)
break
else:
continue
print(f"Your unique id is: {unique_id}")
pickle.dump(unique_id, open(unique_id, 'wb'))
shutil.move(folder, destination)
print("Please choose a NUMBER:")
print("1: New Document")
print("2: Existing Document")
while True:
try:
ask = int(input("Please enter a NUMBER: "))
if ask == 1:
break
elif ask == 2:
break
else:
print("Please enter '1' OR '2'")
continue
except:
print("Please '1' OR '2' NOT a WORD")
if ask == 1:
New_Document()
else:
pass

Trying to store information

I want to save all the users' registered ids to a text file. If the user gives 9 users, then I want it to store all the information. But when I run the code and finish it seems to be overwriting all the information. I know there is something that I'm missing. Can someone please help me?
student_count = int(input("how many students are registering?:"))
reg_form = []
for x in range(student_count):
if student_count >= 0:
registered_user = input("Please enter your I.D number for registeration: ")
id_information = [registered_user + reg_form]
student_count - 1
student_count2 = str(student_count)
with open ("reg_form.txt", 'w') as f:
f.write(registered_user + "\n")
f.close()
Try it that way:
student_count = int(input("how many students are registering?:"))
counter = 0
for x in range(student_count):
if counter < student_count + 1:
registered_user = input("Please enter your I.D number for registeration: ")
counter += 1
reg_form = open('reg_form.txt', 'a')
reg_form.write(registered_user + "\n")
reg_form.close()
You basically do not need reg_form and student_count2.
It appears that you use write mode on every iteration of the loop, which overwrites existing file. You need to use append mode like this:
open('reg_form.txt', 'a')

looping through task numbers

Could someone help with my current code. I would like to add task numbers to my tasks that get saved in my output text document. I would need to loop it so each task will be assigned the next task number. If possible I would like to be able to call on these task numbers later.
My code so far is:
def add_task():
if menu == "a" or menu == "A":
with open( 'user.txt' ) as fin :
usernames = [i.split(',')[0] for i in fin.readlines() if len(i) > 3]
task = input ("Please enter the username of the person the task is assigned to.\n")
while task not in usernames :
task = input("Username not registered. Please enter a valid username.\n")
else:
task_title = input("Please enter the title of the task.\n")
task_description = input("Please enter the task description.\n")
task_due = input("Please input the due date of the task. (yyyy-mm-dd)\n")
date = datetime.date.today()
task_completed = False
if task_completed == False:
task_completed = "No"
else:
task_completed = ("Yes")
with open('tasks.txt', 'a') as task1:
task1.write("\nUser assigned to task:\n" + task + "\nTask Title :" + "\n" + task_title + "\n" + "Task Description:\n" + task_description + "\n" + "Task Due Date:\n" + task_due + "\n" + "Date Assigned:\n" + str(date) + "\n" + "Task Completed:\n" + task_completed + "\n")
print("The new assigned task has been saved")
add_task()
Firstly, I don't really want to go into detail but the way you are storing your output is highly inefficient and difficult to access the larger your text file gets. Why not use some free DB system to store your data.
Secondly. Assuming that you want to write many tasks at a time but only "save" once so to speak, consider using a dict of dicts.
def write_task_to_txt(task):
### break down the dict to from your lines to write to your text
def add_task(task_list,task_id):
new_tasks[task_id] = {}
new_tasks[task_id]["username"] = "username"
new_tasks[task_id]["title"] = "Task 1"
### How you fill up a task
return task_list
new_tasks = {}
for i in range(10):
new_tasks = add_task(new_tasks,i+1)
write_task_to_txt(new_tasks)
With this you can always access the task using new_tasks[task_id] to pull all the data of that task. Note the for loop is using an iterator. If you want to avoid this you could use a global and a while loop instead. BUT if you want to do that, i recommend converting your application into a class and use class variables instead.
Here is a skeleton of how I would try that:
class yourclass():
def __init__(self):
self.task_num = 1 #use 1 if no values
self.tasks_towrite = {}
self.mode_select()
def mode_select(self):
self.menu = input("choose mode")
while(1):
if self.menu == "a" or self.menu == "A":
self.add_task()
if self.menu == "s".casefold() #Cool function that does the same as your menu thingy
self.write_to_text()
else:
print("exit")
self.close_program()
def close_program(self): # Exit function
print("exiting")
sys.exit(1)
def add_task(self): #Add task
with open( 'user.txt' ) as fin :
self.usernames = [i.split(',')[0] for i in fin.readlines() if len(i) > 3]
task = input ("Please enter the username of the person the task is assigned to.\n")
while task not in self.usernames :
task = input("Username not registered. Please enter a valid username.\n")
else:
new_task = {}
new_task["username"] = task
new_task["title"] = input("Please enter the title of the task.\n")
new_task["description"] = input("Please enter the task description.\n")
new_task["due"] = input("Please input the due date of the task. (yyyy-mm-dd)\n")
date = datetime.date.today()
task_completed = False
if task_completed == False:
new_task["completed"] = "No"
else:
new_task["completed"] = "Yes"
new_task["assigned"] = date
self.tasks_towrite[self.task_num] = new_task
sefl.task_num +=1 #New test number
return None #returns to mode_select
def write_to_text(self):
with open('tasks.txt', 'a') as task1:
for i in self.tasks_towrite:
task1.write(str(i) + "\n") #Writes it all at once You can reformat however you want
self.tasks_towrite = {}
print("The new assigned tasks has been saved")
return None #returns to menu or you could go to self.close_program to exit
if __name__== '__main__':
x = yourclass()

Appending multiple values to a list in Python while incrementing through another list

The goal of this is to enter an amount and date for each account on a regular basis. The accounts are static (but more could be appended). What I am trying to do is loop through each account and for each account enter the amount and date. I am doing something wrong, I think in the increment line, and possibly the append line
After that I'd like to print the results to the screen in a way that makes sense (I know what I have is not correct and won't display in a sensible way)
Any ideas? Thanks
account = ['401k', 'RothIRA', 'HSA']
amount = []
date = []
while True:
print('Enter the amount for your ' + account[0] + ' account')
act = input()
amount.append(act)
print('Enter the date for this total')
dt = input()
date.append(dt)
account[] += 1
if act == '':
break
print(account, amount, date)
I think this is what you're trying to do:
i = 0
while i < len(account):
print('Enter the amount for your ' + account[i] + ' account')
act = input()
amount.append(act)
print('Enter the date for this total')
dt = input()
date.append(dt)
i += 1
It's better to use for loop as follows.
for i in account:
print ('Enter amount for your',i,'account')
act = input()
amount.append(act)
print ('Enter date')
dt = input()
date.append(dt)
Also all your lists (account, amount, date) are linked by index. It's much cleaner to use dictionary as someone else posted.
After a slight change in your data structure:
account={ '401K': { 'amount' : 0, 'date': 0 },
'ROthIRA': { 'amount':0, 'date': 0},
'HSA': { 'amount': 0, 'date': 0} }
for eachKey in account.keys:
account[eachKey]['amount'] = input()
account[eachKey]['date'] = input()
print account

Categories