Python 使用.readlines()并努力访问列表

Python 使用.readlines()并努力访问列表,python,python-3.x,text-files,Python,Python 3.x,Text Files,打开文本文件时,我很难访问使用.readlines()创建的列表。文件打开正确,但我不确定如何访问函数“display_clues()”中的列表 错误: Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> display_clues() File "N:\Personal Projecs\game\game.py", line 35, in display_clue

打开文本文件时,我很难访问使用.readlines()创建的列表。文件打开正确,但我不确定如何访问函数“display_clues()”中的列表

错误:

Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
display_clues()
File "N:\Personal Projecs\game\game.py", line 35, in      display_clues
print(clue_list)
NameError: name 'clue_list' is not defined
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
显示线索()
文件“N:\Personal Projecs\game\game.py”,第35行,显示
打印(线索列表)
NameError:未定义名称“线索列表”

谢谢

您必须将列表从
线索\u open()
返回到
显示线索()


作为旁注:我删除了你的坏比无用除了块。永远不要使用裸露的except子句,永远不要假设实际出了什么问题,只捕获您真正可以处理的异常。

您有错误或其他什么吗???或者它刚刚打印了列表我已经编辑了您的代码,现在可以使用了:)您好,谢谢您的回答,文本文件包含数据,当前打印为“无”,文本文件中没有“无”,如何让文本文件打印其中的数据?@user3731036:使用完全相同的代码,我可以获得预期的行为(=>它确实使用python 2.7打印列表-这可能不是您真正想要的,但这不是问题)
def clues_open():
    try:
        cluesfile = open("clues.txt","r")
        clue_list = cluesfile.readlines()
        #print clue_list   #either print the list here
        return clue_list   # or return the list
    except:
        print("Oops! Something went wrong (Error Code 3)")
        exit()
def display_clues():
    clues_yes_or_no = raw_input("Would you like to see the clues? Enter Y/N: ")
    clues_yes_or_no = clues_yes_or_no.lower()
    if clues_yes_or_no == "y":
        clue_list = clues_open()  # catch list here
        print clue_list


display_clues()
def clues_open():
   with open("clues.txt","r") as cluesfile:
       return cluesfile.readlines()

def display_clues(): 
    clues_yes_or_no = input("Would you like to see the clues? Enter Y/N: ")    
    if clues_yes_or_no.lower() == "y": 
        clues_list = clues_open()
        print(clue_list) 
def clues_open():
    try:
        cluesfile = open("clues.txt","r")
        clue_list = cluesfile.readlines()
        #print clue_list   #either print the list here
        return clue_list   # or return the list
    except:
        print("Oops! Something went wrong (Error Code 3)")
        exit()
def display_clues():
    clues_yes_or_no = raw_input("Would you like to see the clues? Enter Y/N: ")
    clues_yes_or_no = clues_yes_or_no.lower()
    if clues_yes_or_no == "y":
        clue_list = clues_open()  # catch list here
        print clue_list


display_clues()