【问题标题】:Python - Input Menu FunctionPython - 输入菜单功能
【发布时间】:2018-03-12 01:08:31
【问题描述】:

我对 Python 还是很陌生,我正在尝试制作一个小冒险游戏,只是为了提高我的技能。所以,对于我的游戏,我希望有几个选项,玩家会选择一个,它会返回不同的结果。但是,选项并不总是相同的,所以我决定制作一个函数,因此选项和结果可能会有所不同。这是我的函数的代码:

def action(act1, act2, act3, act4):
loop = True
while loop:
    print(menu)
    player_action = input("Where would you like to go? ")

    if player_action == '1':
        act1
        return

    elif player_action == '2':
        act2
        return

    elif player_action == '3':
        act3
        return

    elif player_action == '4':
        act4
        return

    else:
        print("Please type \'1\', \'2\', \'3\', or \'4\'")

参数是我要打印的功能。

我的问题是,当我调用这个函数并运行代码时,Python 会在每个 if 和 elif 语句中执行每个函数。例如,当我这样称呼时:

def home_act1():
    print("Welcome to the wilderness!")

def home_act2():
    print("Welcome to the town!")


def home_act3():
    print("Welcome to the store!")


def home_act4():
    print("You left the adventure.")
    exit()


action(home_act1(), home_act2(), home_act3(), home_act4())

我运行程序,它会这样做: 欢迎来到荒野! 欢迎来到小镇! 欢迎光临本店! 你离开了冒险。

进程以退出代码 0 结束

它似乎只是在运行我的所有四个参数,它在我将它变成一个函数之前就可以工作,但有些东西不能正常工作。

感谢任何帮助!

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    在这一行:

    action(home_act1(), home_act2(), home_act3(), home_act4())
    

    您实际上是在调用每个函数并传递结果(在每种情况下都是None,因为这是默认设置。

    尝试只传递函数(home_act 而不是home_act()),然后在循环体中实际调用act()

    【讨论】:

      【解决方案2】:

      您拥有所有 4 个输出然后代码退出的原因是因为您通过执行 action(home_act1(), home_act2(), home_act3(), home_act4()) 立即调用了所有四个 home_act 函数,由于 @987654324 中的 exit() 一个接一个地执行并退出程序@。

      另一件有问题的事情是您在 while 循环中的每个操作之后 return,这意味着一旦用户执行了一个操作,代码就会停止。

      修复这些问题会产生以下代码:

      def action():
          loop = True
          while loop:
              #print(menu)
              player_action = input("Where would you like to go? ")
      
              if player_action == '1':
                  home_act1()  # call the respective action function here
      
              elif player_action == '2':
                  home_act2()
      
              elif player_action == '3':
                  home_act3()
      
              elif player_action == '4':
                  home_act4()
      
              else:
                  print("Please type \'1\', \'2\', \'3\', or \'4\'")
      
      def home_act1():
          print("Welcome to the wilderness!")
      
      def home_act2():
          print("Welcome to the town!")
      
      def home_act3():
          print("Welcome to the store!")
      
      def home_act4():
          print("You left the adventure.")
          exit()
      
      
      action()
      

      祝你进一步编码好运:)

      【讨论】:

        【解决方案3】:
        def home_act1():
            print("Welcome to the wilderness!")
        
        def home_act2():
            print("Welcome to the town!")
        
        
        def home_act3():
            print("Welcome to the store!")
        
        
        def home_act4():
            print("You left the adventure.")
            exit()
        
        
        
        def action():
            loop = True
            while loop:
                # print(menu)
                player_action = input("Where would you like to go? ")
        
                if player_action == '1':
                    return home_act1() #or you can remove the return and carry on in the function
        
                elif player_action == '2':
                    return home_act2()
        
                elif player_action == '3':
                    return home_act3()
                elif player_action == '4':
                    return home_act4()
        
                else:
                    print("Please type \'1\', \'2\', \'3\', or \'4\'")
        
        action()
        

        你可以返回一个函数调用:

        def functionToCall():
            print('Ok function called')
        
        def function():
            return functionToCall()
        
        function()
        

        【讨论】:

          猜你喜欢
          • 2013-10-14
          • 2020-07-17
          • 2011-05-22
          • 2013-11-05
          • 1970-01-01
          • 2023-01-16
          • 1970-01-01
          • 2015-11-22
          • 1970-01-01
          相关资源
          最近更新 更多