将英尺转换为英寸时出现“不支持的操作数类型”错误

时间:2013-09-22 00:07:06

标签: python computer-science

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

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)

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我认为您打算将asking_for_feet传递给convert_feet_to_inches而不是此行中的get_feet函数:

conversion = convert_feet_to_inches(get_feet)

那应该是:

conversion = convert_feet_to_inches(asking_for_feet)

答案 1 :(得分: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)