Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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
(python)append used to list,然后仅返回未使用的值_Python_List - Fatal编程技术网

(python)append used to list,然后仅返回未使用的值

(python)append used to list,然后仅返回未使用的值,python,list,Python,List,标题几乎说明了一切。下面是我写的一个小型日语学习游戏的基本内容。我只需要打印当前循环中尚未打印的假名。有人能看出我做错了什么吗?谢谢各位: #!/usr/bin/python from os import system as cmd from random import choice as random from time import sleep from sys import platform m = ["ma", "mi", "mu", "me", "mo"]

标题几乎说明了一切。下面是我写的一个小型日语学习游戏的基本内容。我只需要打印当前循环中尚未打印的假名。有人能看出我做错了什么吗?谢谢各位:

#!/usr/bin/python
from os     import system as cmd
from random import choice as random
from time   import sleep
from sys    import platform

m = ["ma",  "mi",  "mu",  "me",  "mo"]
y = ["ya",         "yu",         "yo"]
n = ["n"]

def get_dict(group):
    if   group == 1:
        return m
    elif group == 13:
        return y
    elif group == 14:
        return n
    elif group == 15:
        return m + y + n

def clear():
    if "win" in platform: cmd("cls")
    if "lin" in platform: cmd("clear")

def get_kana():
    global kana
    return random(kana)

## Initiate      ##
kana = get_dict(15)
speed = speed()
clear()
print disp
print "Please get ready!..."
sleep(5)

def chk_used():
    global used_kana
    numlpo = 0
    while numlpo < 50:
        numlpo = numlpo + 1
        kana = get_kana()
        if kana not in used_kana:
            used_kana = used_kana.append(kana)
            return kana
            break

def main():
    used_kana = []
    while True:
        clear()
        print disp
        print "Please write the following kana: \n"
        print "    " + chk_used()
        sleep(3)

main()
有几件事:

在chk_used中,您有以下行:

used_kana = used_kana.append(kana)
只有一个问题。list.append返回None。每次执行此操作时,都会将假名附加到used_kana,然后将used_kana的值设置为None

就够了

在函数中定义used_kana

def main():
    used_kana = []
但你试着在全球范围内引用它。Python查找全局变量,但找不到

def chk_used():
    global used_kana
解决方案: 将used kana作为参数传递给chk_used

最后:

    if kana not in used_kana:
        used_kana.append(kana)
        return kana
        break #there's no reason for this break. the return on the line before exits the function, your code will never ever execute this line.

您应该将假名存储在按起始音分类的dict中。@Joel Cornett我曾想过这样做,但我改变了主意,因为我发现在调用自定义组时使用列表要容易得多。不过还是要感谢你的建议:你似乎做错了几件事,最重要的是使用了\u kana=used\u kana.appendkana;这保证会将used_kana设置为None。谢谢!这是非常彻底的,似乎已经解决了我的每一个问题!我在事后增加了闯入作为额外的安全措施,但我明白这不一定会有任何作用。此外,速度并不是我定义的,因为我去掉了10-15个额外的功能,这些功能有助于改变程序的功能。谢谢你的帮助!我现在明白了:
def chk_used(usedCharacters):
    ...

def main():
    used_kana = []
        ...
        print "    " + chk_used(used_kana)
    if kana not in used_kana:
        used_kana.append(kana)
        return kana
        break #there's no reason for this break. the return on the line before exits the function, your code will never ever execute this line.