我哪里出错了 - 不支持的操作数 - : 浮点数和函数

0 投票
1 回答
3654 浏览
提问于 2025-04-18 10:26

我正在从教科书里直接复制代码,但遇到了一个我不知道怎么修复的错误,也看不出哪里出问题了。错误信息是:

Traceback (most recent call last):
File "commission_rate.py", line 79, in <module>
main()
File "commission_rate.py", line 27, in main
pay = (sales * comm_rate) - advanced_pay
TypeError: unsupported operand type(s) for -: 'float' and 'function'

我哪里出错了?这段代码:

'''

Application: commision_rate.py

Description: 

This program calculates a salesperson's pay
at Make Your Own Music

'''

def main():
# Get the amount of sales.
sales = get_sales()

# Get the amount of advanced pay.
advanced_pay = get_advanced_pay

# Determine the commission rate.
comm_rate = determine_comm_rate(sales)

# Calculate pay
pay = (sales * comm_rate) - advanced_pay

# display the amount of pay.
print('The pay is $', format(pay, ',.2f'), sep='')

# determine whether the pay is negative.
if pay < 0:
    print('The salesperson must reimburse')
    print('the company.')

# The get sale function gets the saleperson's
# monthly sales from the user and returns that value.
def get_sales():
# Get the amount of monthly sales.
monthly_sales = float(input('Enter the monthly sales: '))

# return the amount entered
return monthly_sales

# The get advanced pay function gets the amount of
# advanced pay given to the salesperson and returns 
# that amount.
def get_advanced_pay():
# Get the amount of advanced pay.
print('Enter the amount of advanced pay or ')
print('enter 0 if no advanced pay was given.')
advanced = float(input('Advanced pay: '))

# return the amount entered
return advanced

# The determine comm rate function accepts the
# amount of sales as an argument and returns the
# applicable commission rate.

def determine_comm_rate(sales):
#Determines the commission rate
if sales < 10000:
    rate = 0.10
elif sales >= 10000 and sales <= 14999.99:
    rate = 0.12
elif sales >= 15000 and sales <= 17999.99:
    rate = 0.14
elif sales >= 18000 and sales <= 21999.99:
    rate = 0.16
else:
    rate = 0.18

# return the commision rate
return rate

# return main function
main()

我知道缩进有问题,但这是因为编辑器总是把它搞乱。

1 个回答

5

get_advanced_pay 看起来像是一个函数。要调用它,你需要加上括号。

# Get the amount of advanced pay.
advanced_pay = get_advanced_pay()

撰写回答