【问题标题】:Function called without being told to调用函数而不被告知
【发布时间】:2014-11-17 19:22:44
【问题描述】:

python 新手,从事文本冒险,测试函数的使用。

def cell1():
    loop = 1
    while loop == 1:
        print("ONE")
        cave1 = input()
        if cave1 == ("end?"):
            print("\nthis should end program")
            loop = 0
            break
        elif cave1 == ("TWO"):
            global testvar
            testvar = 1
            option1()
        else:
            print("INVALID")

def option1():
    print("TWO")
    loop = 1
    while loop == 1:
        print("test1 definition")
        print (testvar)
        test1 = input()
        if test1 == ("ONE"):
            print("you pick up the cheese")
            loop = 0
            cell1()
        elif test1 == ("THREE"):
            option2()
        else:
            print("INVALID")

def option2():
    print("THREE")
    loop = 1
    while loop == 1:
        print("This is option 3")
        test2 = input()
        if test2 == ("ONE"):
            print("testering2")
            cell1()
        elif test2 == ("TWO"):
            global testvar
            testvar = 2014
            option1()
        else:
            print("INVALID")
run = True
while run == (True):
    print ("testing 123")
    cell1()
    print("restart about to activate")
    cont = input("Restart? ")

    if (cont) != "yes":
       break

这个程序应该允许您在选项(房间是什么)之间切换,最终在 cell1 中,该程序应该是可结束的。

如果程序运行并且“结束?”输入为第一个输入,程序进入底部的继续位,但是,如果您在“房间”之间移动,然后返回单元格 1,输入“结束?”将调用选项 2。

我看了一圈还是觉得莫名其妙,我是不是搞错了什么?

感谢您的帮助,谢谢。

【问题讨论】:

  • 提示:如果cell1调用option1,option1调用cell1,然后cell1结束,option1结束,第一个cell1调用仍会运行。

标签: python function testing


【解决方案1】:

我很确定您遇到的问题是因为当您调用另一个函数时,您并不总是 break 在一个函数中退出循环。例如,如果您的条目是 TWOONE 然后是 end?,您会发现自己仍处于 cell1 循环中。这是因为当对cell1 的内部调用返回时,程序的控制流会回到调用该函数的位置,即option1,因为loop 现在是0,循环结束并@987654330 @ 返回到对 cell1 的外部调用,循环仍在运行。

除非您希望您正在设计的游戏具有树形结构,在此您可以返回到您来自的地方,而不是移动到其他地方,否则我建议您使用不同的架构。而不是你的每个函数在适当的时候调用下一个函数,而是返回那个函数。然后,您将编写一个调用该函数的顶级循环。下面是一个例子,顶层循环调用的函数保存在一个名为state的变量中:

def cell1():
    print("In cell1!")
    while True:
        choice = input("pick 'ONE' or 'TWO' (or type 'quit' to exit):")
        if choice == "ONE":
            return option1
        elif choice == "TWO":
            return option2
        elif choice == "quit":
            return None
        else:
            print("I'm sorry, I didn't understand that.")

def option1():             # these other two functions are very basic in my example
    print("In option1!")   # but you can make them as complex as you want
    return option2

def option2():
    print("in option2!")
    return cell1

def control_loop(initial_state=cell1):
    state = initial_state
    while state is not None:
        state = state() # the next state is the return value of the previous state

【讨论】:

  • 好的,所以我在调用函数后添加了中断,这应该意味着一次只能运行一个函数,对吗?我希望运动就像现实生活中那样,你可以在任何地方移动,只要身体有可能,而不是像一棵树,你必须回溯你的脚步。每次函数调用后中断就足够了吗?
  • 嗯,这将大部分工作,因为它会使外部函数立即返回,而不是循环更多。但是你仍然会无缘无故地构建一堆函数调用。我在回答中建议使用单级架构,尽管可能还有其他方法也可以。
【解决方案2】:

问题是您在嵌套函数中越来越深。例如,改变

if test1 == ("ONE"):
        print("you pick up the cheese")
        loop = 0
        cell1()

if test1 == ("ONE"):
        print("you pick up the cheese")
        loop = 0
        break

将允许您运行程序,进入二号房间,回到一号房间,"end?" 将正常工作。不过,这并不能完全解决您的问题,因为有一个类似的问题,当您从两个变为三个时,如果您只是更改了

if test2 == ("ONE"):
        print("testering2")
        cell1()

if test2 == ("ONE"):
        print("testering2")
        break

它会破坏当前函数并返回到option1()(如果你运行你的程序,去第二个房间,然后到第三个房间,然后回到一个),而"end?" 什么都不做。希望这能让您走上正轨。

【讨论】:

  • 好的,所以我在调用函数后添加了中断,这应该意味着一次只能运行一个函数,对吗?我希望运动就像现实生活中那样,你可以在任何地方移动,只要身体有可能,而不是像一棵树,你必须回溯你的脚步。每次函数调用后中断就足够了吗? ——
【解决方案3】:

"end?" 仅在玩家位于第一个单元格内时才退出的原因是因为您只检查其中的输入。 option1()option2() 中包含的执行不影响cell1() 的执行。您不会从选项函数中返回任何内容,也不会更改哨兵值。

所以,有两种基本方法可以解决这个问题。

首先,您可以从函数中返回一个值:

if option1() == "END":
    break

或者,您可以更改您的 while 循环:

# is_running is defined globally
while is_running:

然后,只要用户键入"end?",就在您的任何方法中将is_running 设置为False。对于您现在使用的设计,这可能是最简单的方法。

不过,我相信您可以看出,通常情况下,随着您添加更多房间并且您的函数调用会进一步嵌套,您的程序会变得更加复杂。

【讨论】:

  • 好的,所以我在调用函数后添加了中断,这应该意味着一次只能运行一个函数,对吗?我希望运动就像现实生活中那样,你可以在任何地方移动,只要身体有可能,而不是像一棵树,你必须回溯你的脚步。每次函数调用后中断就足够了吗? ——
猜你喜欢
  • 1970-01-01
  • 2020-08-05
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多