Python,TypeError:缺少位置语句

Python,TypeError:缺少位置语句,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,我尝试过各种各样的事情,但我不知道我做错了什么。在尝试了5种不同的方法之后,我屈服于在这里寻求帮助。我做错了什么 #tried to do this all sorts of ways but gave up #someone please tell me what the heck I'm doing wrong here CONSTANT_COUNTY_TAX = 0.02 CONSTANT_STATE_TAX = 0.04 def intro(monthSales): pri

我尝试过各种各样的事情,但我不知道我做错了什么。在尝试了5种不同的方法之后,我屈服于在这里寻求帮助。我做错了什么

#tried to do this all sorts of ways but gave up
#someone please tell me what the heck I'm doing wrong here

CONSTANT_COUNTY_TAX = 0.02

CONSTANT_STATE_TAX = 0.04

def intro(monthSales):
    print ("This program shows how many taxes you will have to pay on your sales")

def sales():   
    monthSales = eval(input("Enter the total of sales of this month: "))
    return monthSales

def calcCountyTax(sales):
    sales(monthSales)
    countyTaxDue = sales*CONSTANT_COUNTY_TAX
    return countyTaxDue

def calcStateTax(sales):
    sales(monthSales)
    stateTaxDue = sales*CONSTANT_STATE_TAX
    return stateTaxDue

def total(stateTaxDue, countyTaxDue, monthlSales):
    sales(monthSales)
    calcStateTax(monthSales)
    calcCountyTax(monthSales)
    totalSum = sales - (stateTaxDue + countyTaxDue)
    return totalSum


def main():
    intro()
    sales()
    calcStateTax(monthSales)
    calcCountyTax(monthSales)
    total(monthSales, stateTAxDue, countyTaxDue)

这里有几个问题:

  • 您将
    intro
    定义为使用一个参数
    monthSales
    ,但在
    main
    中,您可以无参数调用它
  • sales
    中,您使用的是
    eval
    而不是真正需要的。请尝试以下方法:

    def sales():   
        monthSales = int(input("Enter the total of sales of this month: "))
        return monthSales
    
  • CalAccountyTax
    中,您使用名为
    sales
    的参数,然后将其视为函数本身。您可以使用函数作为参数,但这可能不是您在这里想要的。也许更像:

    def calcCountyTax(sales):
        countyTaxDue = sales * CONSTANT_COUNTY_TAX
        return countyTaxDue
    
  • 与calcStateTax相同的问题

  • total
    monthSales
    参数是一个输入错误,应该是
    monthSales
  • 如果您正在将预先计算的
    stateTaskDue
    countyTaxDue
    传递到
    total
    函数中,则无需重新计算到期税额。
    total
    功能可以简化:

    def total(monthSales, stateTaxDue, countyTaxDue):
        totalSum = sales - (stateTaxDue + countyTaxDue)
        return totalSum
    
  • main
    方法中,调用函数时没有存储函数返回的值
    main
    应该更像这样:

    def main():
        intro()
        monthlySales = sales()
        monthlyStateTax = calcStateTax(monthlySales)
        monthlyCountyTax = calcCountyTax(monthlySales)
        afterTax = total(monthlySales, monthlyStateTax, monthlyCountyTax)
        print(afterTax)
    
  • 您从不调用
    main
    函数。如果您只是定义它,但从不调用它,那么在运行脚本时不会发生任何事情。在脚本底部定义
    main
    之后,添加:

    def main():
        intro()
        monthlySales = sales()
        ...
        print(afterTax)
    
    main()
    

还有一些其他风格的东西(例如,Python名称通常采用
county\u tax\u due
的形式,而不是驼色的
countyTaxDue
),但我试图特别关注为什么脚本在没有进行其他重大更改的情况下无法工作。

您定义的
def intro(monthSales):
但是在
main()
中调用它时没有参数
intro()

类似地,这里使用参数调用sales函数

def calcStateTax(sales):
sales(monthSales) #
但很明显,函数不接受任何参数

def sales():

此外,您正在不同的函数中使用
monthSales
变量,但未将其定义为全局变量。链接将帮助您了解python变量的各种作用域。

由于其他方法有助于您检查代码,因此这里有一种方法故意过度使用,但对于处理简单的计算问题来说,这是一种更具吸引力的方法

class Tax(object):

    COUNTY = 0.02
    STATE = 0.04

    def __init__(self, sales):
        self.__sales = sales

    @property
    def calc_statedue(self):
        return self.STATE * self.__sales

    @property
    def calc_countydue(self):
        return self.COUNTY * self.__sales

    @property
    def calc_totaldue(self):
        return self.calc_countydue + self.calc_statedue

    @property
    def calc_taxfree_profit(self):
        return self.__sales - self.calc_totaldue


def main():

    print "This program shows how much tax you will need to pay on your sales."
    sales = float(raw_input("Enter the total sales for this month:\n>> "))
    # You can add a check here to see if sales is not non-numerical, perhaps
    # an isdigit, isalpha, float, type, etc. check. Your call.

    tax_calc = Tax(sales)
    print "Taxes due to county: ", tax_calc.calc_countydue
    print "Taxes due to state: ", tax_calc.calc_statedue    
    print "Total taxes due: ", tax_calc.calc_totaldue
    print "Profit after taxes: ", tax_calc.calc_taxfree_profit

if __name__ == "__main__":
    main()
结果:

In [6]: runfile('C:/Users/.../.spyder2/temp.py', wdir='C:/Users/.../.spyder2')
This program shows how much tax you will need to pay on your sales.

Enter the total sales for this month:
>> 1200
Taxes due to county:  24.0
Taxes due to state:  48.0
Total taxes due:  72.0
Profit after taxes:  1128.0

In [7]: 

你确实让我离答案更近了,但我仍然得到了一个令人讨厌的错误回溯(上次的最新调用):文件“/Users/karlmachleidt/Desktop/Lab 2.7.py”,第39行,在main()文件“/Users/karlmachleidt/Desktop/Lab 2.7.py”,第36行,在main postax=total(monthlySales,monthlyStateTax,monthlyCountyTax)文件中“/Users/karlmachleidt/Desktop/Lab 2.7.py”,第27行,总计=sales-(statetasdue+countyTaxDue)类型错误:不支持的操作数类型-:“function”和“float”我算出了,但现在我得到了-10.2。如果我的输入是10,我真的不知道怎么做,我将总计(monthSales…)改为总计(sales…)我明白了,我弄错了输入顺序谢谢你的帮助,Mate我喜欢你的答案,但很遗憾这是一个大学作业,我无法解释这一点,因为我对Python非常陌生