【问题标题】:Creating Decision Tree for Simple Game为简单游戏创建决策树
【发布时间】:2017-10-15 00:49:32
【问题描述】:

我目前是一名学习python的新学生。这是我做大量计算机编码的第一次真实经历。对于我的项目,我必须创建一个具有三个不同难度级别的填空测验。一旦用户选择了难度,游戏应该根据难度打印不同的段落。游戏的每个部分都运行良好,但我在创建“难度选择器”时遇到了麻烦。无论我选择什么难度,游戏都会依次通过简单、中等和困难关卡,然后崩溃。

下面我已经包含了介绍性文字和难度选择器。我很想得到一些帮助。我敢肯定,确实有一些我看不到的明显的东西。谢谢!

    def introduction():
        print '''Welcome to Kevin's European Geography Quizzes. 
        Test your knowledge of European geography. \n'''
        difficulty = raw_input('''Do you want to play an easy, medium, or hard game? 
        Please type the number 1 for easy, 2 for medium, or 3 for hard.\n''' )
        game_chooser(difficulty)

    def game_chooser(difficulty):
        cursor = 0
        difficulty_choice = [easy_game(), medium_game(), hard_game()]
 #each element of the above list links to a procedure and starts one of the 
 #mini-games.
        while cursor < len(difficulty_choice):
            if difficulty != cursor:
                cursor += 1
            else: 
                difficulty_choice[cursor]
                break

【问题讨论】:

  • 您需要一个条件来检查输入。您分享的内容很难给您一个好的答案。

标签: python


【解决方案1】:

如果您只想打印某些内容,但如果您对每个级别都有单独的代码块,则可以使用if else,然后为每个级别定义一个函数并使用此模式:

您可以定义功能块并根据用户输入调用它们,例如:

    # define the function blocks
    def hard():
        print ("Hard mode code goes here.\n")

    def medium():
        print ("medium mode code goes here\n")

    def easy():
        print ("easy mode code goes here\n")

    def lazy():
        print ("i don't want to play\n")

    # Now map the function to user input
    difficulty_choice = {0 : hard,
               1 : medium,
               4 : lazy,
               9 : easy,

    }
    user_input=int(input("which mode do you want to choose : \n press 0 for hard \n press 1 for medium \n press 4 for lazy \n press 9 for easy "))
    difficulty_choice[user_input]()

那么函数块的调用将是:

    difficulty_choice[num]()

【讨论】:

    【解决方案2】:

    为输入添加条件。

    if difficulty == 'easy':
        print("here is an easy game")
    elif difficulty == 'medium':
        print('here is a medium game')
    elif difficulty == 'hard':
        print('here is hard')
    else:
        print('Please enter valid input (easy, medium, hard)')
    

    在每个 if 语句下放置您的游戏代码。

    【讨论】:

    • 这解决了它。我不知道elif。我之前曾尝试过类似于此代码块的内容,除了连续使用三个 if 语句。它没有按预期工作。
    • 很高兴它有帮助!
    【解决方案3】:

    您的代码遇到所有困难的原因是因为这行:

    difficulty_choice = [easy_game(), medium_game(), hard_game()]
    

    当 Python 看到类似 easy_game() 的内容时,它会调用 easy_game 函数并将其替换为结果。不过,您还不想调用该函数,因此您可以去掉括号以仅存储该函数:

    difficulty_choice = [easy_game, medium_game, hard_game]
    

    这意味着您必须在将函数从数组中取出后调用它。

    至于崩溃,当您使用raw_input() 时,您会得到一个字符串。这意味着当您输入1 来决定是否玩简单的游戏时,您会得到字符1,它由数字49 表示。这就是为什么您的代码会经历所有事情并崩溃的原因:您的 1 实际上是 49。事实上,如果你在解释器中输入1 &lt; '1',你会得到True

    要解决这个问题,您可以将raw_input() 的结果传递给int() 函数,该函数将解析它并为您提供正确的整数(如果无法解析,则抛出异常)。 introduction 的最后一行看起来像 game_chooser(int(difficulty))

    你也可以跳过game_chooser 的大部分代码,只需索引数组即可(毕竟这就是它们的用途):

    def game_chooser(difficulty):
        # the lack of parens here means you get the function itself, not what it returns
        difficulty_choice = [easy_game, medium_game, hard_game]
    
        #each element of the above list links to a procedure and starts one of the 
        #mini-games. 
    
        # note the parens to actually call the retrieved function now
        difficulty_choice[difficulty]()
    

    【讨论】:

    • 感谢您的提示。他们回答了我脑海中的很多问题,关于为什么游戏一直在每一个困难中运行。
    • 没问题。我认为获得正确的心智模型很有用,这样您就可以了解您的代码在做什么,特别是如果您想以此为职业。如果您了解正在发生的事情而不是总是猜测,那么您将走得更远(并且更快)。欢迎编程,祝你好运。 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2020-10-11
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多