python - 我的函数中的打印语句出现语法错误。我不知道我做错了什么
问题描述
此代码适用于终端,但不适用于 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))
解决方案
@VPfB 在问题本身的评论中给出了答案。 print
是 Python 3 中的一个函数...它返回一个值,因此return print("foo")
是一个有效的语句。但是在 Python 2 中,print 是一个语句而不是一个函数……它不返回一个值。试图像 in 一样对待它return print("foo")
,会导致解释器出现语法错误。
这就是为什么@JaydeepDevda 询问 OP 他正在运行哪个版本的 Python。我认为他很可能在终端上运行 Python 3,但在 Atom 中运行 Python 2。这可以解释行为上的差异。
推荐阅读
- python - 将击键绑定到 Tkinter 中的按钮
- javascript - 如何修复错误无法读取未定义的属性“修剪”
- swift - Swift 中的 GRDB - .fetchAll 只返回一列
- flutter - 无法使用下拉按钮和未来颤振更新数组
- terminal - 如何从终端(不是 REPL)评估 Julia 表达式?
- windows - 安装winavr时缺少程序员记事本
- python - 一个从列表中挑选东西的 Python 程序
- python - 在 Python 中,为什么不为所有导入的文件将变量声明为全局命名空间
- javascript - 让我的点击滚动功能无限循环的最简单方法?(滚动顶部/动画)
- math - 如何比较两个矩形/大小的比率以确定它们的差异百分比?