Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
坚持使用Python3列表_Python_List - Fatal编程技术网

坚持使用Python3列表

坚持使用Python3列表,python,list,Python,List,Python(3)列表有一些问题 这是在脚本的顶部。后来,它被称为: initLocations() 然后大约4行之后: while currentLoc<20: initLocations() print("Type 'Help' for advice on what to do next.") passedCommand = input("?: ") mainProcess(passedCommand) 非常感谢您的帮助。仅调用函数不会在外部范围内创

Python(3)列表有一些问题

这是在脚本的顶部。后来,它被称为:

initLocations()
然后大约4行之后:

while currentLoc<20:
    initLocations()
    print("Type 'Help' for advice on what to do next.")
    passedCommand = input("?: ")
    mainProcess(passedCommand)

非常感谢您的帮助。

仅调用函数不会在外部范围内创建变量。你必须让他们成为全球性的,但是这是一种非常糟糕的做事方式。您需要从函数中返回它。这是在
initLocations()
中,您需要有一个语句
return locName
,当您调用它时,您需要使用
locName=initLocations()
。假设有两个变量,则需要将它们作为元组发送

演示

然后

while currentLoc<20:
    locName,locDesc = initLocations()
    print("Type 'Help' for advice on what to do next.")
    passedCommand = input("?: ")
    mainProcess(passedCommand)

而函数中定义的currentLoc变量是该函数的局部变量。它们不会泄漏到调用范围(这是一件好事)。如果您希望函数使内容可用,则需要返回它们。例如:

def initLocations():
    locName = […]
    locDesc = […]
    return locName, locDesc
locName, locDesc = initLocations()
这将使函数返回一个包含名称列表和描述列表的两元组。调用函数时,需要捕获值并再次将它们保存到变量中。例如:

def initLocations():
    locName = […]
    locDesc = […]
    return locName, locDesc
locName, locDesc = initLocations()

initLocations
内的范围不是全局范围。在该函数作用域内创建的值在外部封闭的作用域级别中不可用,除非该变量被声明为
global
或由函数作为值返回

第二种方法非常可取:

def initLocations():
    locName = ["The Town","The Blacksmith Hut"]
    locDesc = ["A small but beautiful town. You've lived here all your life.",     
               "There are many shops here, but the Blacksmith Hut is the most intricate."]
    return locName, locDesc
然后,稍后调用该函数时:

locName, locDesc = initLocations()

Python数组有一个更优雅的名称,名为
list
locNam
仅存在于
initLocations()
中,因此如果您在函数外部使用它,它将给您带来该错误。只需在while循环之外声明列表。除非在列表的
initLocations()
中发生了什么,否则使用它是没有意义的。您还需要确保列表中有20个项目。但是,使用函数存储两个列表是没有意义的,在while中声明列表会更有意义。另外,如果OP试图通过调用函数来更改列表,更改将不会持续。我实际上是指在while之外;)是的,只需声明/创建一次列表,如果OP希望在while循环中对其进行手动上载,则更改将被忽略persist@martineau,您可以将列表传递给函数,这比创建一个只返回两个列表的函数更有意义。或者至少在您想要使用它们的函数中创建它们。@Bhargav:好吧,一种面向对象的方法来避免它们,就是使它们成为
游戏
类的实例属性,该类有一个
initLocations(self)
方法来初始化实例属性
self.locName
self.locDesc
。这也可以通过类的
\uuu init\uu()
方法(或对另一个方法的调用)来完成。如果您查看整个代码,所有其他全局级别的变量都可以通过这种方式处理,所有函数都可以生成类方法。唯一全局的东西是新的
游戏
应用程序类的一个实例。
def initLocations():
    locName = ["The Town","The Blacksmith Hut"]
    locDesc = ["A small but beautiful town. You've lived here all your life.",     
               "There are many shops here, but the Blacksmith Hut is the most intricate."]
    return locName, locDesc
locName, locDesc = initLocations()