'Search A Stored List With Similar Values

I have my search function working partially. If I have a stored machine value of 123 it will pull the first 123 from the list rather than every entry that matches 123 like I want it to.

def search_Machine(machine):
    findIdex = 0
    for i in buckets:
        if i[1] == machine:
            return findIdex
        findIdex += 1
    return -1

I need it to search and show every entry with the matching value the user inputs. If that makes sense.

elif a == 5:
    Machine = input("Enter machine: ")
    import os

    os.system('cls' if os.name == 'nt' else "printf '\033c'")
    bktIndex = search_Machine(Machine)
    if bktIndex >= 1:

Entire script below:

buckets = []
count = 0


def export_bkt(buckets):
    fopen = open("PushoutBuckets.txt", "a")
    for bucket in buckets:
        for i in bucket:
            fopen.write(i + " ")
        fopen.write("\n")
    fopen.close()


def import_bkt(fname):
    temp = []
    fopen = open(fname, "r")
    for line in fopen:
        line = line.strip(' ')
        if len(line) != 0:
            line = line.split(',')
            temp.append(line)
    fopen.close()
    return temp


def Add_Bucket():
    global count
    Bucket = []
    count += 1
    print('Enter group number: ', end='')
    Bucket.append(input())
    print('Enter machine: ', end='')
    Bucket.append(input())
    print('Enter hookup type: ', end='')
    Bucket.append(input())
    print('Enter capacity: ', end='')
    Bucket.append(input())
    print('Enter width: ', end='')
    Bucket.append(input())
    print('Enter profile: ', end='')
    Bucket.append(input())
    print('Enter weight: ', end='')
    Bucket.append(input())
    print('Enter notes: ', end='')
    Bucket.append(input())
    print('Enter coupler type: ', end='')
    Bucket.append(input())
    print('Enter large notes: ', end='')
    Bucket.append(input())
    buckets.append(Bucket)
    import os
    os.system('cls' if os.name == 'nt' else "printf '\033c'")


def view_all_Buckets(buckets):
    for i in buckets:
        print('--------------------------------------------------------------------------------')
        print('Group Number: ', i[0])
        print('Machine: ', i[1])
        print('Pin On / Coupler: ', i[2])
        print('Capacity: ', i[3])
        print('Width: ', i[4])
        print('Profile: ', i[5])
        print('Weight: ', i[6])
        print('Notes & Updates: ', i[7])
        print('Coupler Type: ', i[8])
        print('Large Notes: ', i[9])
        print('--------------------------------------------------------------------------------')


def search_GroupNumber(groupnumber):
    findIdex = 0
    for i in buckets:
        if i[1] == groupnumber:
            return findIdex
        findIdex += 1
    return -1


def search_Machine(machine):
    findIdex = 0
    for i in buckets:
        if i[1] == machine:
            return findIdex
        findIdex += 1
    return -1


def main():
    global count
    global buckets


while True:
    print("----------------------------- Pushout Buckets ----------------------------------\n")
    print('Enter -1 to exit')
    print('\nThere are (', count, ') buckets in the database.')
    print('--------------------------------------------------------------------------------')
    print('1. Add new bucket.\n')
    print('2. View all buckets.\n')
    print('3. Edit bucket information.\n')
    print('4. Search bucket by group number.\n')
    print('5. Search bucket by machine.\n')
    print('6. Search bucket by hookup type.\n')
    print('7. Search bucket by capacity.\n')
    print('8. Search bucket by width.\n')
    print('9. Search bucket by profile / fixture.\n')
    print('10. Search bucket by weight.\n')
    print('11. Search bucket by coupler type.\n')
    print('12. Search bucket by notes & updates.\n')
    print('13. Export bucket information into a text file.\n')
    print('14. Import bucket information from a text file.')
    print('--------------------------------------------------------------------------------\n')
    print('Please enter your option number: ', end="")
    a = int(input())
    import os

    os.system('cls' if os.name == 'nt' else "printf '\033c'")
    if a == -1:
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
        quit()
    elif a == 1:
        Add_Bucket()
        print()
    elif a == 2:
        view_all_Buckets(buckets)
        print()
        input("Press ENTER to return to the main menu.")
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
    elif a == 3:
        Machine = input("Enter the group number of the bucket you would like to update: ")
        bktIndex = search_Machine(Machine)
        if bktIndex >= 0:
            buckets[bktIndex][0] = input("Enter Updated Group Number: ")
            buckets[bktIndex][1] = input("Enter Updated Machine: ")
            buckets[bktIndex][2] = input("Enter Updated Pin On / Coupler: ")
            buckets[bktIndex][3] = input("Enter Updated Capacity: ")
            buckets[bktIndex][4] = input("Enter Updated Width: ")
            buckets[bktIndex][5] = input("Enter Updated Profile: ")
            buckets[bktIndex][6] = input("Enter Updated Weight: ")
            buckets[bktIndex][7] = input("Enter Updated Notes & Updates: ")
            buckets[bktIndex][8] = input("Enter Updated Coupler Type: ")
            buckets[bktIndex][9] = input("Enter Updated Large Notes: ")
            print("\nBucket information has been updated successfully.\n")
            input("Press ENTER to return to the main menu.")
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
        else:
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
            print("Bucket with " + Machine + " is not found.\n")
            input("Press ENTER to return to the main menu.")
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
    elif a == 4:
        GroupNumber = input("Enter Group Number: ")
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
        bktgroup = search_GroupNumber(GroupNumber)
        if bktgroup >= 0:
            print("------------------------------------------------------------------------------")
            print('Group Number:', buckets[bktgroup][0])
            print('Machine:', buckets[bktgroup][1])
            print('Pin On / Coupler:', buckets[bktgroup][2])
            print('Capacity:', buckets[bktgroup][3])
            print('Width:', buckets[bktgroup][4])
            print('Profile:', buckets[bktgroup][5])
            print('Weight:', buckets[bktgroup][6])
            print('Notes & Updates:', buckets[bktgroup][7])
            print('Coupler Type:', buckets[bktgroup][8])
            print('Large Notes:', buckets[bktgroup][9])
            print("------------------------------------------------------------------------\n")
            input("Press ENTER to return to the main menu.")
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
        else:
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
            print('Invalid Option.\n')
            input("Press ENTER to return to the main menu.")
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
    elif a == 5:
        Machine = input("Enter machine: ")
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
        bktIndex = search_Machine(Machine)
        if bktIndex >= 1:
            print("------------------------------------------------------------------------------")
            print('Group Number:', buckets[bktIndex][0])
            print('Machine:', buckets[bktIndex][1])
            print('Pin On / Coupler:', buckets[bktIndex][2])
            print('Capacity:', buckets[bktIndex][3])
            print('Width:', buckets[bktIndex][4])
            print('Profile:', buckets[bktIndex][5])
            print('Weight:', buckets[bktIndex][6])
            print('Notes & Updates:', buckets[bktIndex][7])
            print('Coupler Type:', buckets[bktIndex][8])
            print('Large Notes:', buckets[bktIndex][9])
            print("------------------------------------------------------------------------\n")
            input("Press ENTER to return to the main menu.")
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
        else:
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")
            print("Machine " + Machine + " not found.\n")
            input("Press ENTER to return to the main menu.")
            import os

            os.system('cls' if os.name == 'nt' else "printf '\033c'")


    elif a == 13:
        export_bkt(buckets)
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
        print("Data Successfully Exported.\n")
        input("Press ENTER to return to the main menu.")
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
    elif a == 14:
        buckets = import_bkt("PushoutBuckets.txt")
        count = count + len(buckets)
        print("Data Successfully Loaded.\n")
        input("Press ENTER to return to the main menu.")
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
    else:
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")
        print('Invalid Option.\n')
        input("Press ENTER to return to the main menu.")
        import os

        os.system('cls' if os.name == 'nt' else "printf '\033c'")

main()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source