Home » Python » python – How do I fix a required positional argument?-Exceptionshub

python – How do I fix a required positional argument?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’m working on an employee management system and am running into an issue with positional arguments. I’m pretty new to coding and this is a project for class. I have followed some tutorials and books but I keep getting this error:

Traceback (most recent call last):
  File "C:\Users\jmcra\Desktop\CPT200\Employment Management System - Functionality 4.1.py", line 152, in <module>
    searchEmployeeSSN()
TypeError: searchEmployeeSSN() missing 1 required positional argument: 'isForEdit'

I have tried several remedies but it just causes more errors. Would someone please help me:

  1. Understand what this means, and
  2. Solve my personal situation?

Any help is GREATLY appreciated!

#initialize list to store employee' information
employee_info = [ ]
#initialize global variable to save the SSN
employeeSSN = 0
#initialize global variable to save the index of the found record
employeeIndex = 0
#clear the screen
def cls():
    print('\n' * 50)

#displaythe employee information in the required format
def employeeFormatedInfo(name, ssn, phone, email, salary):
    print('')
    print('--------------- {0:s} --------------------'.format(name))
    print('SSN: {0:s}'.format(ssn))
    print('Phone: {0:s}'.format(phone))
    print('Email: {0:s}'.format(email))
    print('Salary: ${0:s}'.format(salary))
    print('------------------------------------------')
    print('')

#view all employees in the system
def viewEmployeeInfo():
    cls()
    print('------------------------------------------')
    print('          View all employees\n')
    print('------------------------------------------\n')
    if(len(employee_info)==0):
        print('No employees entered.\n')
    else:
        for i in range(0, len(employee_info)):
            line = employee_info[i].split(',')
            employeeFormatedInfo(line[0], line[1], line[2], line[3], line[4])
    input('Press any button to go to Main Menu')
    cls()

#add employee
def addEmployee():
    cls()
    print('-------------------------------------------')
    print('            Add Employee')
    print('-------------------------------------------\n')
    try:
        #users can enter employee Name, SSN, Phone, Email and Salary
        name=input('Employee Name: ')
        ssn=input('Employee SSN: ')
        phone=input('Employee phone number: ')
        email=input('Employee email: ')
        salary=input('Employee salary: $')
        line = name +',' +ssn +',' +phone +',' +email +',' +salary
        index = len(employee_info)
        employee_info.insert(index, line)
    except:
        cls()
        addEmployee()
    print('\nEmployee info has been added!!\n')
    print('--------------------------------------------\n')
    try:
        #users can add new employee or return to Main Menu
        option=input('Enter q/Q to return to Main Menu or A to add another employee')
        if option.lower() == 'q':
            cls()
        else:
            cls()
            addEmployee()
    except:
        cls()
        addEmployee()
#display the Main Menu
def printOptions():
    print('--------- Employee Management System -------\n')
    print('There are ({0:2d} ) employees in the system.'.format(len(employee_info)))
    print('--------------------------------------------\n')
    print('1. View all employees\n')
    print('2. Add new employee\n')
    print('3. Search employee by SSN\n')
    print('4. Edit employee information\n')
    #check the user choice
    try:
        answer=int(input('Please enter your choice by number: '))
    except ValueError:
        print('Not a number')
        return 100
    print('--------------------------------------------\n')
    return answer

def searchEmployeeSSN(isForEdit):
    cls()
    print('--------------------------------------------\n')
    print('        Search for Employee by SSN\n')
    print('--------------------------------------------\n')
    if(len(employee_info)==0):
        input('No employee in the list.\n')
        cls()
    else:
        try:
            ssn=input('Enter SSN to search for employee or q/Q to exit to Main : ')
            global employeeSSN
            employeeSSN = ssn
            if ssn.lower() == 'q':
                return 0

        except ValueError:
            searchEmployeeSSN(0)
    for i in range(0, len(employee_info)):
        line = employee_info[i].split(',')
        if(line[1] == ssn):
            global employeeIndex
            employeeIndex = i
            employeeFormatedInfo(line[0], line[1], line[2], line[3], line[4])
            break
        else:
            print('\n       No employees with that SSN.\n')
            return 0
        try:
            if (isForEdit == 0):
                option=input('Enter q/Q to exit or any key to add another employee:')
                if option.lower() == 'q':
                    cls()
                else:
                    searchEmployeeSSN(0)
        except:
            cls()

def editEmployeeInfo():
    cls()
    #reuse the searchEmployeeSSN
    result = searchEmployeeSSN(1)
    if(result != 0):
        name=input('Employee new Name: ')
        phone=input('Employee new phone number: ')
        email=input('Employee new email: ')
        salary=input('Employee new salary: $')
        #delete the old information
        del employee_info[employeeIndex]
        line = name +',' +employeeSSN +',' +phone +',' +email +',' +salary
        #add the new information
        employee_info.insert(employeeIndex, line)
        input('\nEmployee information has been updated.  Please press any key to go to the Main Menu.\n')

while True:
    cls()
    mode = printOptions()
    if mode == 1:
        cls()
        viewEmployeeInfo()
    if mode == 2:
        cls()
        addEmployee()
    if mode == 3:
        cls()
        searchEmployeeSSN()
    if mode == 4:
        cls()
        editEmployeeInfo()
How to&Answers:
def searchEmployeeSSN(isForEdit):
searchEmployeeSSN()

Those two lines are inconsistent. You’re defining the function to require a parameter yet calling it without a parameter.

There are a couple of ways to fix this depending on your situation.

  1. Perhaps the easiest is to simply pass a parameter. You’ve already done that in several places, though you pass 1 and 0 rather than True and False, something you may wish to fix.
  2. You could also give the parameter a default value (for the most likely case), something like def searchEmployeeSSN(isForEdit = False):.

Answer:

1. Understanding what this means

Let’s break down the error that you’re getting.

TypeError: searchEmployeeSSN() missing 1 required positional argument: ‘isForEdit’

This is an error relating to the function searchEmployeeSSN. An argument is a value passed to a function when calling it, so we need to look at where we are calling the function.

In Python, you call a function by writing its name followed by some parentheses, like so: searchEmployeeSSN(). So what could be wrong with this? Arguments go inside the brackets, and the error message tells you that the code “searchEmployeeSSN()” is missing 1 argument.

You defined the function like so:

def searchEmployeeSSN(isForEdit):
    ...

Here, searchEmployeeSSN has one parameter called isForEdit. When you call the function with an argument, searchEmployeeSSN(5), the parameter isForEdit is assigned the value of the parameter.

You can also specify default values for parameters like this:

def searchEmployeeSSN(isForEdit=10):
    ...

Parameters that are not given a default value are called positional arguments, because they are only assigned values based on the position of the argument passed to the function; in your case, isForEdit is the first parameter and thus is assigned the value of the first argument. Positional arguments are required because otherwise Python wouldn’t know what value to assign them.

Parameters that are given a default value are called keyword arguments, because they can also be assigned a value using the name of the parameter:

def f(x, m=5, c=0):
    return m * x + c

f(10) # 50
f(10, 6) # 60
f(10, m=6) # 60
f(10, c=5) # 55

2. Solving your issue

I hope that you can see already what you need to do. In your while True block at the bottom, you call searchEmployeeSSN() without passing any arguments. You must pass an argument here or change the function such that it has no positional arguments.

I would also advise using Boolean values for variables that represent a yes/no, true/false kind of value. In Python, you can use the values True and False.