0
import os.path
import re
def request ():
    print ("What file should I write to?")
    file = input ()
    thing = os.path.exists (file)
    if thing == "true":
        start = 0
    elif re.match ("^.+.\txt$", file):
        stuff = open (file, "w")
        stuff.write ("Requests on what to add to the server.")
        stuff.close ()
        start = 0
    else:
        start = 1
    go = "yes"
    list1 = (start, file, go)
    return list1
start = 1
while start == 1:
    request ()
    (start, file, go) = list1

我尝试返回 get list1 以返回并在循环中解压缩它,以便我可以设置 while 循环之后的变量。每当我尝试运行它并输入“Thing.txt”时,我都会得到NameError: name 'list1' is not defined. 我在这里错过了什么吗?

4

1 回答 1

1

尝试这个:

# -*- coding: utf-8 -*-
#!/usr/bin/python


import os.path
import re
def request ():
    print ("What file should I write to?")
    file = input ()
    thing = os.path.exists (file)
    # thing is a boolean variable but not a string, no need to use '=='
    if thing:
        start = 0
    elif re.match ("^.+.\txt$", file):
        stuff = open (file, "w")
        stuff.write ("Requests on what to add to the server.")
        stuff.close ()
        start = 0
    else:
        start = 1
    go = "yes"
    list1 = (start, file, go)
    return list1
start = 1
while start == 1:
    # you need to get return value of function request
    list1 = request ()
    (start, file, go) = list1
    # Or you can simply write this way  (start, file, go) = request()
于 2014-12-30T06:13:06.337 回答