-2

The idea is that the function "search()" should take the name of a customer and a list of customers as it's parameters and check whether the customer was in the customer.txt (then return True) or not (then return False).

The specifications are: 1) take the name of a customer and a list of customers (the list made by writeData(), customer.txt) as its parameters 2) check if name is in the list 3) If it is, return True ; if not, return False.

Currently,
1) whenever I type S in the menu it tells me it is missing two required positional arguments

Traceback (most recent call last):

    cdman()
  File "test.py", line 55, in cdman
    menu()
  File "test.py", line 24, in menu
    search()
TypeError: search() missing 2 required positional arguments: 'name' and 'filename'

2) If I simply call "search() " in the shell, it says " name "search" is not defined ", both before and after calling " cdman() " first

3) If I were to have the name " Jacob Walker " in the file and attempt to search for it, it will highlight Walker and say invalid syntax

def cdman():

    def menu():

        print('*------------------------*')
        print('| Customer Database Menu |')
        print('*------------------------*\n')

        choice = input('D: Create/Update Customer Data\nR: Display Database\nS: Search for User\nE: End Program\n')
        if choice == 'D':
            writeData()
            menu()
        elif choice == 'S':
            search()
            menu()
        elif choice == 'R':
            readData()
            menu()
        elif choice == 'E':
            print('PROGRAM TERMINATED')

    def writeData():
        while True:
            database = open('customer.txt', 'a')
            name = input('Name: ')
            if name == '-1':
                database.close()
                return False
            else:
                id_num = input('ID: ')
                database.write('Name: ' + name + ' ')
                database.write('ID: ' + id_num + '\n')

    def readData():
        database = open('customer.txt', 'r')
        for line in database:
            print(line)

    def search(name, filename):
        if name in open(filename).read():
            print('True')
        else:
            print('False')

    menu()
cdman()
stovfl
  • 14,998
  • 7
  • 24
  • 51

2 Answers2

0

First of all, you are calling the search() function in your code without parameters, but in its definition you're passing two parameters.

So, after typing 'S', your program expects a search(CustomerName, FileName), but not search().

elif choice == 'S':
   search(CustomerName, FileName)
   menu()

I guess your search() function can't read all the available lines in the file. Try to use the following code for your search() function:

def search(CustomerName, filename):
    with open(filename, "r") as fp:
        Customer_Names = [name.rstrip('\n') for name in fp.readlines()] 
    for AvailableName in Customer_Names:
        if AvailableName == CustomerName:
            print True
        else:
            print False 
Kian
  • 1,319
  • 1
  • 13
  • 23
0

Your problem is that you are attempting to call your function search without passing in the arguments you have told it it needs (name and filename)

This stack overflow question has a lot of in depth explanation of python function parameters, and would be a helpful read, as well as this article. To address the error you are running into, there are 2 basic solutions.

The first solution (which is more helpful based on what your code does) is to actually pass in the parameters. Where you have

elif choice == 'S':
   search()
   menu()

You should be telling it what to search for, i.e.

elif choice == 'S':
   search('Bob', 'customer.txt')
   menu()

You can also give your parameters default values, the way its written now

 def search(name, filename):

It is expecting to receive those values, otherwise they will not be defined (the error you are running in to)

You can tell it the default value, to and handle cases where nothing gets passed in, like this:

  def search(name=None, filename='customer.txt'):
        if name is None:
           print('No name provided!')
        elif name in open(filename).read():
            print('True')
        else:
            print('False')
Richard Stoeffel
  • 695
  • 8
  • 13
  • Thank you for your help! I changed my search option in the menu() to: elif choice == 'S': name = input('Name: ') filename = input('Filename: ') search(name, filename) menu() – jwalker0498 Oct 30 '18 at 15:12