【问题标题】:Defining lists and calling functions in Python在 Python 中定义列表和调用函数
【发布时间】:2018-10-06 14:28:50
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 你应该避免使用except:。在这种情况下,您应该使用except ValueError:,因为如果输入了无效的浮点数,就会出现这种情况。 except: 将捕获所有内容,您将无法看到其他错误。
  • 如果您事先知道条目数,请使用[None]*number_of_entries,否则请使用append(),如答案之一所述。

标签: python python-3.x list append


【解决方案1】:

该行有一个IndexError: list assignment index out of range

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

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

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

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

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

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

【讨论】:

  • raw_input 在 Python 3 中?
【解决方案2】:

您不能只为列表分配一个不存在的索引来向其中添加项目:

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

相反,您应该考虑使用.append() 方法来处理列表。

(您没有看到IndexError 调试详细信息的原因是因为您的except 子句阻止它显示。裸except 子句通常被认为是反模式,因为它们掩盖了这样的意外错误并使其更难判断出了什么问题 - 他们捕获了任何异常,而不仅仅是由于用户输入错误而导致的异常。)

【讨论】:

    猜你喜欢
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2017-04-22
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多