简体   繁体   English

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

[英]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! 我正在使用Python3。这是一个班级的家庭作业项目,但我没有寻找任何可以解决我问题的方法! 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. 现在,我可以在第一个输入中输入名称,但是一旦按下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

I know this is a total mess but any help at all would be so appreciated! 我知道这是一团糟,但是任何帮助都将不胜感激!

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. 相反,您应该查看对列表使用.append()方法。

(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.) (之所以看不到IndexError调试详细信息,是因为您的except子句阻止了它的显示。裸except子句通常被认为是反模式,因为它们掩盖了此类意外错误,并且更难于指出出了什么问题-他们抓住了任何异常,不仅是由于用户输入错误而引起的异常。)

There is an IndexError: list assignment index out of range for the line 有一个IndexError: list assignment index out of range该行的IndexError: list assignment index out of range

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM