连续重新运行while循环直到某个输入

时间:2014-02-21 21:16:13

标签: python

我正在尝试做的是询问用户两个输入,然后在输入上调用一个函数。如果它没有引发异常,则再次执行while循环。如果它确实引发异常而不是打印某些内容并再次执行while循环。我遇到的问题是,如果没有引发异常,我找不到再次执行while循环的方法。如果确实引发了异常,它就有效。我能想到的唯一方法就是我把#Rerun循环粘贴到整个True循环的地方,但在这种情况下这真的很糟糕。

class Illegal(Exception):
    pass

def add_input(first_input, second_input):
    if first_input + second_input >= 10:
        print('legal')
    else:
        raise Illegal

def play():
    inp = input("Ready? <yes/no>: ")
    if inp == 'yes':
        while True:
            first_input = input("First number: ")
            second_input = input("Second number: ")
            try:
                if first_input or second_input == 'quit':
                     break
                else:
                     add_input(int(first_input), int(second_input))
                     #Rerun loop
            except Illegal:
                print("Illegal")
            else:
                break
    else:
        return

   >>> play()
       Ready? <yes/no>: yes
       First number: 1
       Second number: 2
       Illegal
       First number: 9
       Second number: 6
       legal
       First number: 1
       Second number: 2
       Illegal
       First number: quit

我的想法是什么:

def play():
    inp = input("Ready? <yes/no>: ")
    if inp == 'yes':
        while True:
            first_input = input("First number: ")
            second_input = input("Second number: ")
            try:
                if first_input or second_input == 'quit':
                    break
                else:
                    add_input(int(first_input), int(second_input))
                    while True:
                            first_input = input("First number: ")
                           except Illegal:
                                print("Illegal move")
                            else:
                                break
            except Illegal:
                print("Illegal move")
            else:
                break
    else:
        return                     second_input = input("Second number: ")
                            try:
                                if first_input or second_input == 'quit':
                                    break
                                else:
                                    add_input(int(first_input), int(second_input))
                                    #Rerun loop

但这是一个可怕的想法,因为那时我必须不断粘贴相同的东西。

2 个答案:

答案 0 :(得分:1)

break中的else:会导致循环退出。你可以完全删除它:

else:
    break

答案 1 :(得分:0)

您希望在用户未通过键入quit退出循环时运行循环。因此,您只需删除此else: break

def play():
    inp = input("Ready? <yes/no>: ")
    if inp.lower() == 'yes':
        while True:
            first_input = input("First number: ")
            second_input = input("Second number: ")
            # quit if the user wants
            if first_input or second_input == 'quit':
                break

            try:
                add_input(int(first_input), int(second_input))
            except Illegal:
                print("Illegal")

另外,请习惯将最少的代码放在try/except块中。它使调试更容易。你的上一个else: return也没用,你可以删除它。