i need help with my program

First of all, you really need to delimit your records in product.txt rather than having them all strung together. Split is the function you're looking for, but again it will split based on whatever delimiter you use and the way you have the file formatted you can only use spaces as delimiter, so without some manipulation it's going to be difficult to get THAT data to do what you want.

For your search function to work you must divide the file contents into records and there are MANY ways to do this. I'm going to show you just one VERY simple method for doing what you need.

def main(): f=open('products.txt') #opens the file fileContents=f.read() #read file contents #split into 4 strings. You need comma's between your individual #records like this (1 doodad 3.50 50, 2 gizmo 1.75 30, 3 gadget 2.99 45, 4 lipstick 4.25 75) allRecords=fileContents.split(',') record=[] #define a list record=allRecords #assign the split result to the list f.close() #close the file

endit = False #loop terminator
while endit == False:
    print('1: [D]ispaly all products:')
    print('2: [S]earch products:')
    print('3: E[x]it:')
    s=input('Enter Selection >>:')

    if s == '1' or s == 'D' or s == 'd':
        print('****************')
        print(fileContents)
        print('****************')
        print()
        print()
    elif s == '2' or s == 'S' or s == 's':
        q=input('Enter Product Number >>:')
        if q.isnumeric():
            if q <= str(len(record)):
                print('****************')
                print(record[int(q)-1])
                print('****************')
                print()
                print()
    elif s == '3' or s == 'X' or s == 'x':
        endit = True
    else:
        print('That\'s not a valid menu option!')

main()

There are ways to massage that big data string into records without delimiting the records but no one would ever store data that way anyway as records are ALWAYS delimited, so I don't see the value in trying to explain that.

/r/Python Thread