2017-10-15 103 views
0

我目前是一名新学生学习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 
+0

你需要一个条件来检查输入。它很难给你一个很好的答案与你分享。 – Joe

回答

0

为输入添加条件。

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语句下放置您的游戏代码。

+0

这解决了它。我不知道elif。之前我曾尝试类似于此代码块的内容,除了连续使用三条if语句。它没有按预期工作。 –

+0

很高兴帮助! – Joe

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]() 
0

原因你的c颂遍历所有的困难,因为这行:

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 < '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]() 
+0

感谢您的提示。他们回答了我脑海中的很多问题,这些问题是为什么游戏在各种困难中持续运行。 –

+0

没问题。我认为找到正确的思维模型是很有用的,这样你就可以理解你的代码在做什么,特别是如果你想从事这个职业。如果你明白发生了什么,而不是总是猜测,那么你会走得更远(更快)。欢迎编程,祝你好运。 =) –