[英]How does one continue code from the line where the function was on in Python?
我的文字游戏中有一个nameType()
函数,并且使用了两次:
这是此职位针对的当前代码(可能会更改):
def TitleScreen():
blankHuge()
anar()
print('The Game of Hierarchy and Anarchy\nBy Roach\nText Edition\n▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\nType "new" to start a new game.\nTo load a game, type "load".\nTo exit, type "exit".')
StartGame = input()
if StartGame == 'new':
print('► Crew Member: Hello, fine man. I can see you are on an adventure to the land of Salbury.\nBefore you disembark the boat, please enter your name.')
for filename in file:
os.unlink(filename)
nameType()
global PlayerIG
PlayerIG = Player(CharName)
print('► Crew Member: Thanks for travelling with us, and have a great time,',PlayerIG.name,'.')
saveGame()
MainMenu()
# more stuff that aren't related down here...
这是nameType()
函数:
def nameType():
print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬')
CharName = input()
if CharName != '':
if len(CharName) > 20:
print('► The name is too long!')
nameType()
else:
print('► Are you sure this is what you want as a name?\nIf so, type "yes".\nOtherwise, type "no".')
confirmName = input()
if confirmName == 'yes': # If this is true, then jump to next codeline from where the nameType() func was placed...
pause() # Doesn't work
if confirmName == 'no':
nameType()
else:
print('► Invalid Input!')
nameType()
这是应该从原处继续执行代码的函数,但是它不起作用:
def pause():
pause = input('► Press Enter to continue.')
if pause != '':
pause()
有什么办法可以继续执行我的代码?
示例(很可能无法正常工作):
def TitleScreen():
...
nameType() # Someway, jump to next line after code in nameType() has been executed...
global PlayerIG
PlayerIG = Player(CharName)
...
saveGame()
MainMenu()
def management():
...
opt = input()
if opt = 'name':
nameType() # Someway, jump to next line after code in nameType() has been executed...
PlayerIG.nameWait = 12
PlayerIG.heat = 0
...
注意:我对这将如何工作有一个想法,但是我不确定它是否会工作。 它是:
# outside of nameType()
if confirmName == 'yes':
...
我的想法奏效了, if
在nameType()
函数外部使用, if
确实可行。 我使用了global
(我知道这是不好的编码,但是不管用什么),现在该函数起作用了。
def nameType():
global confirmName
global CharName
print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬')
CharName = input()
if CharName != '':
if len(CharName) > 20:
print('► The name is too long!')
nameType()
else:
print('► Are you sure this is what you want as a name?\nIf so, type "yes".\nOtherwise, type "no".')
confirmName = input()
# Items down here for the confirmName input removed
# This is the system for the 'jump':
if confirmName == 'blah1':
# add stuff here...
if confirmName == 'no':
# add stuff here...
else:
# add stuff here...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.