Its about the pay system of a shop.
The user enters a product ID,the interpreter gets the details from the file and appends it to a list and continues till the products are over.

If the user can't enter the ID he can add the product name directly which will get the details from the file and append to list again.

From the list the total price will be displayed and the product names which will be in the file.

There must also be a fucntion to delete a product from the list and make new sum accordingly.

def ID_search(prod_id):

    infile=open("tab.txt","r")

    line=infile.readline()
        
    while line!="":
        x=string.split((line),",")
        if prod_id==float(x[0]):
            print "Product is", x[1]
        line=infile.readline()
        
    infile.close()

This is the function i've used for search the ID from the file and display result..Please need help from here on.

Editor's note:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here

[/code]

Dear Feenix45,
The best way is to enclose the code in bracket to maintain the
Indentation which is vita important. Just wrap your code tags


Is this what you originally wrote??
Is your application GUI or Console?

def ID_search(prod_id):
    infile=open("tab.txt","r")
    line=infile.readline()
    while line!="":
        x=string.split((line),",")
        if prod_id==float(x[0]):
            print "Product is", x[1]
            line=infile.readline()
    infile.close()

Also, you open the file and read the entire file for each ID entered. This is time consuming (in computer time) and so you should consider reading the file once and placing the records in a dictionary, indexed on ID, and using the dictionary to look up the info as it is entered.

hmm its GUI but about dictionary never did this can u help me with the indexes and dictionary plz.

Also, you open the file and read the entire file for each ID entered. This is time consuming (in computer time) and so you should consider reading the file once and placing the records in a dictionary, indexed on ID, and using the dictionary to look up the info as it is entered.

Can u guide me with this please?

Or just tell me how to start please

Here's an example of reading the file into a dictionary:

def load_IDs():
    infile=open("tab.txt","r")
    lines=infile.readlines()
    infile.close()
    my_IDs = {}
    for line in lines:
        data=line.strip().split(",")
        my_IDs[ data[ 0 ] ] = data[ 1 ]
    return my_IDs

Here's an example of reading the file into a dictionary:

def load_IDs():
    infile=open("tab.txt","r")
    lines=infile.readlines()
    infile.close()
    my_IDs = {}
    for line in lines:
        data=line.strip().split(",")
        my_IDs[ data[ 0 ] ] = data[ 1 ]
    return my_IDs

Thanx but this means i copy the file on a list.

Can i work directly from the file?

You're reading the contents of the file into a dictionary, not a list.

What do you mean work directly from the file?

You're reading the contents of the file into a dictionary, not a list.

What do you mean work directly from the file?

No i dont know how to work with dictionary.

all the data are stored on a text file which im using though its a bit more time consuming.

Is there a way that i can do product search?i.e i enter the product name and it displays the price.
The program must search the price in the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.