在Python中,如何从函数所在的行继续编写代码?

在Python中,如何从函数所在的行继续编写代码?,python,function,class,variables,Python,Function,Class,Variables,我的文本游戏中有一个nameType()函数,它被使用了两次: 创建一个stats类(或其他)以便程序可以保存它们,以及 游戏中用于某些变量更改的函数 以下是此帖子针对的当前代码(可能会发生更改): 下面是nameType()函数: def nameType(): print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬') CharName = input() if CharName != '': if len(CharName) > 20: print('► The

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

  • 创建一个stats类(或其他)以便程序可以保存它们,以及
  • 游戏中用于某些变量更改的函数
  • 以下是此帖子针对的当前代码(可能会发生更改):

    下面是
    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()
    
    有没有办法继续我的代码

    示例(最有可能不起作用):

    注意:我知道这将如何工作,但我不确定它是否会工作。它是:

    # outside of nameType()
    if confirmName == 'yes':
        ...
    

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

    您需要将游戏状态存储在某个位置。您将从该存储中读取该状态,并以该状态开始游戏。您能清除您想要/需要执行的操作吗?@erip我已存储了游戏状态。而且,这不是我的问题。我需要做的是找到一种方法,从放置
    nameType()
    函数的位置跳转到下一行代码。我没有回答您的问题,因为您正在尝试解决一个问题。如果您有游戏状态,您可以将其传递到适当的类中,这些类将处理拾取游戏中的最后一个检查点。
    # outside of nameType()
    if confirmName == 'yes':
        ...
    
    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...