Python "Function call with parameter: Converting measurements?"

Python "Function call with parameter: Converting measurements?"

我正在上我的第一门编程课程,如果这是一个愚蠢的 question/possibly 在此网站上分类错误的类别,我深表歉意。我正在研究的练习题之一如下:

定义函数 print_total_inches,参数为 num_feet 和 num_inches,打印总英寸数。注意:一英尺有 12 英寸。例如:
print_total_inches(5, 8) 打印:
总英寸:68

所附照片是我执行的代码以及我收到的错误。非常感谢任何 help/resources。谢谢!

您的代码无法格式化输入(同时缺少所需的计算)。

def print_total_inches (num_feet, num_inches):
    print('Total inches:', num_feet * 12 + num_inches)

print_total_inches(5, 8)

def total_inches (num_feet, num_inches):
    return num_feet * 12 + num_inches

print('Total inches:', total_inches(5, 8))

应该做 - 当 return.

没有什么可做的时候不需要 return
def print_total_inches(num_feet, num_inches):
    totalinches = (12*num_feet) + num_inches
    print('total inches:' + str(totalinches))

print_total_inches(5, 8)

您不需要 return,正如您的函数名称所说 print_total_inches

或者您也可以使用 format 来打印

print('total inches:{0}'.format(total_inches))