python - (Python) 转到函数内的一行

标签 python function line goto

所以,我已经开始了:

def pickClass():
    print('What class are you? FIGHTER, MAGE, or THIEF?')
    classChoice = raw_input()
    if classChoice == 'FIGHTER':
        print('You are a mighty warrior. Are you sure? YES or NO.')
        _confirm = raw_input()
        if _confirm == 'YES':
            print('So be it.')
        elif _confirm == 'NO':
            pickClass()
        else:
            print('YES or NO only!')
            #go back to '_confirm = raw_input()'

我卡住的部分在最后 -- 我如何在不再次遍历整个函数的情况下转到代码的特定部分?

(我知道这个打印品有点多余,但无论如何,maaaaan)

最佳答案

您将需要重组您的函数。尝试这样的事情:

def pickClass():
    valid_classes = ["FIGHTER", "MAGE", "THIEF"]
    while True:
        print('What class are you? FIGHTER, MAGE, or THIEF?')
        classChoice = raw_input()
        if classChoice not in valid_classes:
            print("Invalid class")
        else:
            print("Are you sure you want to be a %s?" % classChoice)
            while True:
                _confirm = raw_input()
                if _confirm == 'YES':
                    print('So be it.')
                    return classChoice
                elif _confirm == 'NO':
                    break
                else:
                    print('YES or NO only!')

关于python - (Python) 转到函数内的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13486070/

相关文章:

python - Flask SocketIO 的正确配置

python - 计算矩阵行之间的余弦距离

php - 无法从循环中为数组赋值

python - 如何制作一个函数,一次返回一个列表中的项目?

java - Graphics2D 或 2D Components 的度量单位是什么?

python - 为什么 .rstrip ('\n' ) 不起作用?

python - 编辑字符串 python 正则表达式 python

javascript - jsLint 显示 "somefunction() was used before it was defined"错误

Java:查找文件的最后一行是否为空

Python Pandas : find max values in a cell