0

标题基本概括了所有内容。以下是我编写的一个小型日语学习游戏的准系统。如果它尚未在当前循环中打印,我只需要打印假名。谁能看到我做错了什么?谢谢 :)

#!/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()
4

1 回答 1

0

几件事:

chk_used中,您有以下行:

used_kana = used_kana.append(kana)

只有一个问题。list.append()返回无。每次执行此操作时,您都附加kanaused_kana,但随后您将值设置used_kana为无。

used_kana.append(kana)

就足够了

used_kana在函数中定义

def main():
    used_kana = []

但是您尝试在全局范围内引用它。Python 寻找一个全局变量,却找不到。

def chk_used():
    global used_kana

解决方案:将使用的假名作为参数传递给chk_used()

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.
于 2012-05-03T09:25:08.603 回答