DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Pay system (http://www.daniweb.com/forums/thread157696.html)

Feenix45 Nov 16th, 2008 12:47 pm
Pay system
 
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.

Feenix45 Nov 16th, 2008 12:49 pm
Re: Pay system
 
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]

evstevemd Nov 16th, 2008 3:29 pm
Re: Pay system
 
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()

woooee Nov 16th, 2008 5:06 pm
Re: Pay system
 
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.

Feenix45 Nov 17th, 2008 12:49 pm
Re: Pay system
 
hmm its GUI but about dictionary never did this can u help me with the indexes and dictionary plz.

Feenix45 Nov 18th, 2008 10:39 am
Re: Pay system
 
Quote:

Originally Posted by woooee (Post 737374)
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

jlm699 Nov 18th, 2008 1:07 pm
Re: Pay system
 
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

Feenix45 Nov 18th, 2008 1:12 pm
Re: Pay system
 
Quote:

Originally Posted by jlm699 (Post 738827)
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?

jlm699 Nov 18th, 2008 2:30 pm
Re: Pay system
 
You're reading the contents of the file into a dictionary, not a list.

What do you mean work directly from the file?

Feenix45 Nov 18th, 2008 10:06 pm
Re: Pay system
 
Quote:

Originally Posted by jlm699 (Post 738890)
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.


All times are GMT -4. The time now is 8:47 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC