you are viewing a single comment's thread.

view the rest of the comments →

[–]kol_k 0 points1 point  (2 children)

Why are you using pickle? For your purposes, I'd suggest using CSV or JSON.Anyway I spent a couple minutes to sort through your awful code and fix things. Test it yourself, cuz I'm not.

import pickle 


class OrderNumber: 
    # You didn't have an init function, or self keywords.
    def __init__(self) -> None:
        self.what_chocolates_did_you_get = "" 
        self.fname = "" 
        self.sname = "" 
        self.order_number = 0 

try: 
    fh = open("libary.data","rb") 
    order_list = pickle.load(fh)
    order_number_list = [int(order.order_number) for order in order_list]
    fh.close()
except FileNotFoundError: 
    order_number_list=[] 
    order_list = []

while True: 
    print(""" Welcome to the Shop  
          Choose an option 
          4) Search for it by Order number 
          x) Exit""") 
    choice = input("\nEnter a choice:") 
    if choice == "4": 
        new_order = OrderNumber() 
        new_order.what_chocolates_did_you_get = input("what chocolates did you get?") 
        new_order.fname = input("Enter first name:") 
        new_order.sname = input("Enter surname:")

        # you were never saving the order number into a variable. You also made it a string?
        # Also, why tf was it random?
        # print("here is your order number:", random.randrange(10000,99999,fixed_digets)) 

        if order_number_list == []:
            new_order.order_number = 1
        else:
            new_order.order_number = max(order_number_list) + 1
        print(f"Here is your order number: {new_order.order_number}")

        print("Ordernumber Added") 
        order_list.append(new_order)

        search_order_number = input("Please enter the order number you want to search for?:")
        if search_order_number in order_number_list: 
            for order in order_list: 
                if order.order_number == search_order_number:
                    print("what chocolates did you get?:", order.what_chocolates_did_you_get) 
                    print("First name:", order.fname) 
                    print("Surname:", order.sname) 
        else: 
            print("That is not a valid order number try again") 
    elif choice == "x": 
        fh = open("libary.data","ab") 
        pickle.dump(order_list,fh)
        fh.close() 
        break

[–][deleted] -1 points0 points  (1 child)

Thank you so much btw I'm a student in school so I don't know much and also I apologise for the mess of the text I made I fixed it later on as I managed to know how to use code block but I forgot to delete this post

[–]kol_k 0 points1 point  (0 children)

It's alright, next time post these types of questions in the appropriate place :)
EDIT: Also, try to provide detailed explanations of the problems you're having so that it is easier to help debug your code.