-3

我正在处理作业问题,并收到以下错误:

Traceback (most recent call last):

File "/Users//Dropbox/Homework 3 - 2.py", line 15, in <module>
    conversion = convert_feet_to_inches(get_feet)   
File "/Users//Dropbox/Homework 3 - 2.py", line 4, in convert_feet_to_inches
    calculate_conversion = feet*12 TypeError: 
unsupported operand type(s) for *: 'function' and 'int'

这是我的代码。我正在尝试将英尺转换为英寸:

def convert_feet_to_inches(feet):
    calculate_conversion = feet*12
    return calculate_conversion

def get_feet():
    ask_for_feet = float(input("Please enter number of feet for conversion "))
    return ask_for_feet

def printing_answer():
    print (convert_feet_to_inches)

asking_for_feet = get_feet()
conversion = convert_feet_to_inches(get_feet)
print_answer(printing_answer)

我究竟做错了什么?

4

2 回答 2

1

我认为你的意思是传递asking_for_feetconvert_feet_to_inches而不是这一行中的函数get_feet

conversion = convert_feet_to_inches(get_feet)

所以应该是:

conversion = convert_feet_to_inches(asking_for_feet)
于 2013-09-22T00:10:24.793 回答
0

错误是因为get_feet()是一个函数,而您将它传递给了它,convert_feet_to_inches()它接受了您传递的参数并将其乘以 12。您不能将函数乘以 int,这就是错误所说的。我想你的意思是通过asking_for_feet。所以改变

conversion = convert_feet_to_inches(get_feet)

conversion = convert_feet_to_inches(asking_for_feet)

之后,你有

print_answer(printing_answer)

该功能print_answer尚未定义,因此请更改:

def printing_answer():
    print (convert_feet_to_inches)

def print_answer(answer):
    print (answer)

然后你的最后一行代码是:

print_answer(conversion)
于 2013-09-22T00:46:11.507 回答