如何使代码向后跳转而不是向前跳转?

How to make the code jump back instead of forward?

我正在处理一项策划任务,但我的一个定义函数没有按预期工作。

如何让“退出”跳回到“猜一猜……”而不是继续上色?

def valid_usercolour():
    while True:
        #print('Welcome to the Mastermind') 
        usercolour = input('Please have your guess [r,o,y,g]: ').lower()
        if 'quit' in usercolour:    
            while True:
                dc_quit = input('Do you really wanted to quit the game?[y/n]: ')
                if dc_quit.lower() == "y":
                    print()
                    print('Alright, thank You for playing, Goodbye', user_name, '!' )
                    quit()
                    break 
                elif dc_quit.lower() == "n":
                    print("Alright, let's continue to our game")
                    break
                else:
                    print("Sorry! I don't understand what you mean, could you please type only [Y/N]")
                    continue
                      
        colourslist = ['r','o','y','g']
        if any(character not in colourslist for character in usercolour):
            print("Error! Only Characters ,", colourslist, "are allowed")
            continue
        if len(usercolour) != 4:
            print("Error! Only 4 characters are allowed!")
            continue
        break
    return usercolour

只需添加 else: 并在 colourslist 之前缩进其下方的内容。 Python代码不“跳回”:

def valid_usercolour():
    while True:
        #print('Welcome to the Mastermind') 
        usercolour = input('Please have your guess [r,o,y,g]: ').lower()
        if 'quit' in usercolour:
            while True:
                dc_quit = input('Do you really wanted to quit the game?[y/n]: ')
                if dc_quit[0].lower()[0] == "y":
                    print()
                    print('Alright, thank You for playing, Goodbye', user_name, '!' )
                    quit()
                    break 
                elif dc_quit[0].lower()[0] == "n":
                    print("Alright, let's continue to our game")
                    break
                else:
                    print("Sorry! I don't understand what you mean, could you please type only [Y/N]")
                    continue
        else:                      
            colourslist = ['r','o','y','g']
            if any(character not in colourslist for character in usercolour):
                print("Error! Only Characters ,", colourslist, "are allowed")
                continue
            if len(usercolour) != 4:
                print("Error! Only 4 characters are allowed!")
                continue
            break
    return usercolour

奖励:我在 dc_quit 的值处添加了 [0] 以强制只取一个字符。完整的 'yes' 或 'no' 也有效 ;-)