我的文字游戏中有一个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':
...
答案 0 :(得分:0)
我的想法很有效,在if
函数之外使用nameType()
工作。我使用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...