Python 为简单游戏创建决策树

Python 为简单游戏创建决策树,python,Python,我目前是一名学习python的新生。这是我第一次真正体验计算机编码。对于我的项目,我必须在三个不同难度级别的空白测验中填写。一旦用户选择了难度,游戏应根据难度打印不同的段落。游戏的每一部分都很好,但是我在创建“难度选择器”时遇到了困难。无论我选择了什么难度,游戏都会依次通过简单、中等和困难级别,然后崩溃 下面我已经包括了介绍性文本和难度选择器。我想得到一些帮助。我确信有一些很明显的事情我看不到。谢谢大家! def introduction(): print '''Welc

我目前是一名学习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
def简介():
欢迎来到凯文的欧洲地理测验。
测试你的欧洲地理知识\不
难度=原始输入(''你想玩一个简单的、中等的还是硬的游戏?
请键入数字1表示简单,2表示中等,或3表示硬。\n“”
游戏选择器(难度)
def游戏选择器(难度):
光标=0
难度选择=[easy\u game()、medium\u game()、hard\u game()]
#上面列表中的每个元素都链接到一个过程并启动其中一个
#迷你游戏。
当光标
为输入添加条件

如果难度==“容易”:
打印(“这是一个简单的游戏”)
elif难度==“中等”:
打印('这是一个中等游戏')
elif难度==‘困难’:
打印('这里很难')
其他:
打印('请输入有效输入(简单、中等、硬)')

在每个if语句下输入游戏代码

如果您只想打印某些内容,但如果每个级别都有单独的代码块,则可以使用
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]()

您的代码遇到所有困难的原因在于这一行:

difficulty_choice = [easy_game(), medium_game(), hard_game()]
当Python看到类似于
easy\u game()
的内容时,它调用
easy\u game
函数并用结果替换它。但是,您还不想调用该函数,因此可以去掉括号来存储函数:

difficulty_choice = [easy_game, medium_game, hard_game]
这意味着您必须在将函数从数组中取出后调用它

至于崩溃,当您使用
raw\u input()
时,会返回一个字符串。这意味着当你输入
1
来决定一个简单的游戏时,你会得到角色
1
,它由数字
49
表示。这就是为什么你的代码会经历所有的事情并崩溃:你的
1
实际上是一个
49
。事实上,如果您在解释器中键入
1
,您将返回
True

要解决这个问题,您可以将
raw\u input()
的结果传递给
int()
函数,该函数将对其进行解析并给出正确的整数(如果无法解析,则抛出异常)。
简介的最后一行
看起来像
游戏选择器(int(难度))

您也可以跳过
game\u chooser
的大部分代码,只需将其索引到数组中即可(毕竟,这就是它们的用途):


您需要一个条件来检查输入。很难用你分享的东西给你一个好的答案,这就解决了问题。我不知道埃利夫的事。我以前尝试过类似于这个代码块的东西,除了一行中有三个if语句。它没有按预期工作。很高兴它起了作用!谢谢你的提示。他们回答了我脑海中的很多问题,关于为什么游戏一直在克服每一个困难。没问题。我认为获得正确的心智模型是很有用的,这样你就可以理解你的代码在做什么,特别是当你试图从中获得一份职业的时候。如果你明白发生了什么而不是总是猜测,你会走得更远(更快)。欢迎来到编程,祝你好运。=)
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]()