所以,我已经开始了:
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/