0

这是我用作示例的程序的简化版本。

我知道使用 GOTO 是不好的做法,因为它会导致代码草率和混乱,但是它非常适合解决我遇到的这个问题(问题在帖子底部详细说明)。

def prompt():
    while True:
        user_input = raw_input:
        if input == '?':
            print help_o
        elif not user_input.isalpha():
            print "You entered bad characters"
        elif user_input == 'r': ##Restart
            ???????????
        else:
            return user_input


load_word_list() ##Load words into list

for word in wordList: ##Loop that needs to restart
    for i in range(5):
        to_speak = "Spell, %s" %word
        subprocess.Popen(['espeak', to_speak])
        answer = prompt()
        if answer != word:
            print "You got it wrong"


#Print results

在提示符处,我想重新加载 wordList 列表并重新启动外部 for 循环。

使用 GOTO,我可以代替 ????... GOTO load_word_list()。

但既然这是 Python(而且 Python 是关于好代码的),那么解决这个问题的 Python 方法是什么?

4

5 回答 5

3

您可以从以下位置返回一个元组prompt()

    elif user_input == 'r': #Restart
        return True, None
    else:
        return False, user_input

    restart, answer = prompt()
    if restart:
        break
    if answer != word:
        print "You got it wrong"
于 2012-09-21T00:17:07.537 回答
1

另一种 jsbuenos 解决方案。这实际上重新运行外部 for 循环。

def prompt():
    while True:
        user_input = raw_input()
        if input == '?':
            print help_o
        elif not user_input.isalpha():
            print "You entered bad characters"
        elif user_input == 'r': #Restart
            raise RestartException
        else:
            return user_input


class RestartException(Exception):
    pass


while True:
    load_word_list() ##Load words into list
    try:
        for word in wordList: ##Loop that needs to restart
            for i in range(5):
                to_speak = "Spell, %s" %word
                subprocess.Popen(['espeak', to_speak])
                answer = prompt()
                if answer != word:
                    print "You got it wrong"
    except RestartException:
        # catch the exception and return to while loop
        pass
    else:
        # exit while loop
        break
于 2012-09-21T02:27:12.123 回答
1
class RestartException(Exception):
    pass

def prompt():
    while True:
        user_input = raw_input:
        if input == '?':
            print help_o
        elif not user_input.isalpha():
            print "You entered bad characters"
        elif user_input == 'r': #Restart
            raise RestartException
        else:
            return user_input


load_word_list() ##Load words into list

for word in wordList: ##Loop that needs to restart
    try:
        for i in range(5):
            to_speak = "Spell, %s" %word
            subprocess.Popen(['espeak', to_speak])
            answer = prompt()
            if answer != word:
                print "You got it wrong"
     except RestartException:
        pass
于 2012-09-21T00:19:16.933 回答
0
def prompt():
    while True:
        user_input = raw_input("enter words")
        if input == '?':
            print help_o
        elif not user_input.isalpha():
            print "You entered bad characters"
        elif user_input == 'r': #Restart
            return False
        else:
            return user_input


answer = True

while answer == True:
    load_word_list() ##Load words into list

    for word in wordList: ##Loop that needs to restart
        for i in range(5):
            to_speak = "Spell, %s" %word
            subprocess.Popen(['espeak', to_speak])
            answer = prompt()
            if answer == False:
                answer = True # reset for the while loop
                break # break to while loop
            elif answer != word:
                print "You got it wrong"

print results
于 2012-09-21T01:14:53.693 回答
0

我不太了解您想要做什么,但是您可以删除特殊情况并直接在 for 循环中prompt处理特殊情况'r'

于 2012-09-21T00:16:55.100 回答