我遇到了一个错误,其中python 3.7中的代码块导致多个输出,其中一个是预期的。代码如下:
def main():
length=0;height=0;width=0;volume=0
length,height,width=getinput(length,height,width)
volume=processing(length,height,width,volume)
output(volume)
def getinput(length,height,width):
length, height, width = input("Enter length, height, and width:").split(',')
length = int(length)
height = int(height)
width = int(width)
return length,height,width
def processing(length,height,width,volume):
volume = length * height * width
return length,height,width,volume
def output(volume):
print("the volume of the prism is:", volume)
main()
输出应为:
the volume of the prism is: 400
输出结果为:
the volume of the prism is: (20, 10, 2, 400)
答案 0 :(得分:1)
在def processing(length,height,width,volume)
函数中,return语句为return length,height,width,volume
,这基本上意味着您返回tuple
,当您将其捕获到名为volume的变量中时,它将变为元组。请在输出中查看(20, 10, 2, 400)
。大括号显示它是tuple
。您也可以通过打印type(volume)
来确认。如果您想要400
作为答案,请执行output(volume[3])
。
答案 1 :(得分:0)
您可能打算这样做:
>>> def processing(length,height,width,volume):
... volume = length * height * width
... return volume
...
>>> main()
Enter length, height, and width:20, 10, 2
the volume of the prism is: 400