python - 在Python中定义列表和调用函数

标签 python python-3.x list append

我正在使用 Python 3。这是一个类(class)的作业项目,但我并不是在寻找东西来帮我解决问题!我只是想知道是否有人可以帮助准确指出我哪里出了问题,或者我需要研究什么才能使我的代码正常工作。

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()

现在,我可以在第一个输入中输入一个名称,但是一旦我按下回车键,它就会打印出我的错误代码,然后打印出最终的函数,如下所示。

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

我知道这完全是一团糟,但如果有任何帮助,我们将不胜感激!

最佳答案

该行存在IndexError:列表分配索引超出范围

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

您没有看到它,因为您排除了所有错误并且没有打印它们。我建议使用

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

等等。请注意,我将其 append 到列表中。我还建议采用不同的错误处理策略。

或者,您可能希望使用字典而不是使用两个列表,因为您希望将收入与姓名关联起来,

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

关于python - 在Python中定义列表和调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50033671/

相关文章:

python - Tensorflow 0.7.1 与 Cuda 工具包 7.5 和 cuDNN 7.0

python - Tkinter 中 Listbox 和 Radiobutton 触发的事件

python - 不同行的正则表达式 - python

python - 使用 Pandas/Python 在数据框中查找最接近的匹配数字

python - 这个任务是什么? Python

python - 如何使用随机列表中的特定数字打印python执行时间以输出时间和生成的随机数?

python - 文本字符串到唯一整数的方法?

python - Python 中列表项的自定义迭代

java - 无法获取Java中特定索引中的对象

java - 检测 JSON 对象列表中的重复条目