首页 > 解决方案 > 我的函数中的打印语句出现语法错误。我不知道我做错了什么

问题描述

此代码适用于终端,但不适用于 atom。我在返回打印行收到语法错误。对不起,如果很明显,我是编程新手。

#returns footage stretched


material_feet = input('Material Feet: ')
material_inches = input('Material Inches: ')
actual_feet = input('Actual Feet: ')
actual_inches = input('Actual Inches: ')

def Stretch(material_feet, material_inches, actual_feet, actual_inches):
    material_total = material_feet * 12 + material_inches
    actual_total = actual_feet * 12 + actual_inches

    amount_stretched = (actual_total - material_total) * 12 / actual_total
    difference = divmod((actual_total - material_total), 12)
    return print('Material stretched total of ' + str(difference) + ' and stretched ' + str(amount_stretched) + ' per foot.')

Stretch(int(material_feet), int(material_inches), int(actual_feet), int(actual_inches))

标签: pythonsyntax-error

解决方案


@VPfB 在问题本身的评论中给出了答案。 print是 Python 3 中的一个函数...它返回一个值,因此return print("foo")是一个有效的语句。但是在 Python 2 中,print 是一个语句而不是一个函数……它不返回一个值。试图像 in 一样对待它return print("foo"),会导致解释器出现语法错误。

这就是为什么@JaydeepDevda 询问 OP 他正在运行哪个版本的 Python。我认为他很可能在终端上运行 Python 3,但在 Atom 中运行 Python 2。这可以解释行为上的差异。


推荐阅读