Lamebryant
Lamebryant

Reputation: 21

Defining lists and calling functions in Python

I am using Python 3. This is for a homework project for a class, but I'm not looking for something to do the problem for me! I was just wondering if someone could help point out exactly where I've gone wrong, or what I need to look into to make my code work.

def main():

    taxPayersList = []
    incomesList = []
    taxesList = []

    taxPayersList, incomesList = inputNamesAndIncomes(taxPayersList, incomesList)

    taxesList = calculateTaxes(incomesList)
    displayTaxReport(taxesList, incomesList, taxPayersList)


def inputNamesAndIncomes(taxPayersList, incomesList):
    print('Welcome to Xanadu Tax Computation Program')
    print('')
    confirmation = input('Do you have income amounts? y/n ')
    index = 0

    try:

        while confirmation == 'y' or confirmation == 'Y':
            taxPayersList[index] = input('Enter a tax payer name: ')
            incomesList[index] = float(input('Enter income amount: '))

            confirmation = input('Are there more income amounts? ')
            index += 1
    except:
        print('An error occurred. Please only enter numbers for income amount.')

    return taxPayersList, incomesList

def calculateTaxes(incomesList):

    index = len(incomesList)

    while index < len(incomesList):
        if incomesList[index] >= 0 and incomesList[index] <= 50000:
            taxesList[index] = incomesList[index] * .05

        elif incomesList[index] >= 50000 and incomesList[index] <= 100000:
            taxesList[index] = 2500 + ((incomesList[index] - 50000) * .07)

        elif incomesList[index] >= 100000:
            taxesList[index] = 6000 + ((incomesList[index] - 100000) * .09)

        index += 1

    return incomesList    


def displayTaxReport(taxesList, incomesList, taxPayersList):
    print('2018 TAX DUES FOR XANADU STATE')
    print('')
    print('Name\t\tANNUAL INCOME\tTAXDUE')
    for n in incomesList:
        print(taxPayersList,'\t\t',incomesList,'\t',taxesList)


main()

Right now, I can enter a name into the first input, but as soon as I hit enter it just prints out my error code and then print out the final function like below.

Welcome to Xanadu Tax Computation Program

Do you have income amounts? y/n y
Enter a taxpayer name: Susan
An error occurred. Please only enter numbers for income amount.
2018 TAX DUES FOR XANADU STATE

Name        ANNUAL INCOME   TAXDUE

I know this is a total mess but any help at all would be so appreciated!

Upvotes: 1

Views: 58

Answers (2)

innisfree
innisfree

Reputation: 2485

There is an IndexError: list assignment index out of range for the line

taxPayersList[index] = input('Enter a tax payer name: ')

You didn't see it because you excepted all errors and didn't print them. I suggest using

name = input('Enter a tax payer name:')
taxPayersList.append(name)

etc. Note that I append it to the list. I also suggest a different strategy of handling errors.

Alternatively, you might wish to use a dictionary instead of using two lists, since you want to associate an income with a name,

name = input('Enter a tax payer name:')
income = float(input('Enter income amount:'))
incomes[name] = income

Upvotes: 1

Amber
Amber

Reputation: 526553

You can't just assign into a non-existant index for a list to add items to it:

>>> a = []
>>> a[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

Instead you should look at using the .append() method for lists.

(The reason you aren't seeing the IndexError debugging details is because your except clause prevents it from being displayed. Bare except clauses are often considered an antipattern because they mask unexpected errors like this and make it harder to tell what went wrong - they catch any exception, not just ones due to bad user input.)

Upvotes: 1

Related Questions