【问题标题】:How does one continue code from the line where the function was on in Python?如何从 Python 中函数所在的行继续代码?
【发布时间】:2017-11-05 14:43:58
【问题描述】:

我的文字游戏中有一个nameType()函数,用了两次:

  1. 创建一个统计类(或其他)以便程序可以保存它们,并且
  2. 游戏中用于更改某些变量的函数。

这是这篇文章所针对的当前代码(可能会发生变化):

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':
    ...

【问题讨论】:

  • 您需要在某处存储游戏状态。您将从该商店读取该状态并以该状态开始游戏。
  • 你能把你想要/需要做的事情弄清楚吗?
  • @erip 我已经存储了游戏状态。另外,这不是我的问题。我需要做的是找到一种方法,从放置nameType() 函数的位置跳转到下一行代码。
  • 我没有回答您的问题,因为您正在尝试解决XY problem。如果你有游戏状态,你可以将它传递给适当的类来处理游戏中的最后一个检查点。

标签: python function class variables


【解决方案1】:

我的想法奏效了,在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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2019-10-13
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多