【发布时间】:2019-03-11 12:55:58
【问题描述】:
所以我编写了一个简单的程序来根据用户输入计算立方体的体积。我有一个 main.py 和一个 volume.py。在 main.py 中,我简单地调用了相应的函数 cubeVolume。但是,当我尝试将生成的多维数据集体积从 volume.py 附加到 main.py 中名为 cubeList 的列表中时,我得到一个未定义的错误。我该如何解决这个问题?
#main.py
from volume import cubeVolume
import math
cubeList = []
userInput = str(input("Enter the shape you wish to calculate the volume for: "))
userInput = ''.join(userInput.split()).lower().capitalize()
while userInput != "Q" or userInput != "Quit":
if userInput == "C" or userInput == "Cube":
cubeSide = int(input("Enter the side length for the Cube: "))
cubeVolume(cubeSide)
cubeList.append(volume)
这是volume.py文件
#volume.py
import math
def cubeVolume(side):
volume = side**3
print("The volume of the cube with side length {} is: {}".format(side, volume))
这是我的输出:
Enter the shape you wish to calculate the volume for: cube
Enter the side length for the Cube: 3
The volume of the cube with side length 3 is: 27
Traceback (most recent call last):
File "/Users/User/Desktop/folder2/main.py", line 14, in <module>
cubeList.append(volume)
NameError: name 'volume' is not defined
【问题讨论】:
-
你需要在你的 cubeVolume 函数的末尾定义一个
return
标签: python list function append