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
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
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')
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()
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