2017-11-11 97 views
-4

我是新来的StackOverflow(第一次发布)和新的编码与蟒蛇。目前通过Udacity参加课程。我很难与我们获得的这个课程的一个项目,并决定来这里看看是否有人可以帮助。这个Python测验让我疯狂

该项目将创建一个需要玩家正确回答的4个空白测验。需要将测验打印出正确的答案,但我很难将其正确打印出来。

我的代码如下。将不胜感激任何帮助或建议,我可以得到这一点。

谢谢!

easy_quiz = "If you ever get stuck, check out the __1__ for common 
    problems students face when attempting this project. If you need 
    additional help, you can schedule a 1:1 appointment with one of our 
    __2__ to get you un-stuck. This project should be __3__. If at any time 
    it becomes not fun, take a step back, deep breath, and ask for __4__!. 
    \n\n" 
    easy_answers = ["forums", "mentors", "fun", "help"] 

    medium_quiz = "Game must have 3 or more levels and each level contains 4 or more __1__ to fill in. Immediately after running the program, user is prompted to select a difficulty level from easy/__2__/hard. Once a level is selected, game displays a fill-in-the-blank and a prompt to fill in the first one. When player guesses __3__, new prompt shows with correct answer in the previous blank and a new prompt for the next blank. When player guesses __4__, they are prompted to try again. \n" 
medium_answers = ["blanks", "medium", "correctly", "incorrectly"] 

hard_quiz = "__1__ are used as __2__ to automate tasks which are likely to be repeated. Functions produce the appropriate output (typically with a __3__ statement) from the appropriate input (function parameters). Your code should take advantage of __4__ and variable names should reflect the values they store. \n" 
hard_answers = ["Functions", "tools", "return", "variables"] 

blanks = ["__1__", "__2__", "__3__", "__4__"] 

difficulty = raw_input("\nChoose your difficuty level = easy, medium, or hard? ") 
print "" 
if difficulty == "easy": 
    quiz = easy_quiz 
    answers = easy_answers 
    print "You chose easy!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + easy_quiz 

elif difficulty == "medium": 
    quiz = medium_quiz 
    answers = medium_answers 
    print "You chose medium!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + medium_quiz 

elif difficulty == "hard": 
    quiz = hard_quiz 
    answers = hard_answers 
    print "You chose hard!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + hard_quiz 

def word_in_pos(word, parts_of_speech): 
    for pos in parts_of_speech: 
     if pos in word: 
      return pos 
    return None 

def play_game(quiz, parts_of_speech): 
    replaced = [] 
    i = 0 
    quiz = quiz.split() 
    for word in quiz: 
     replacement = word_in_pos(word, parts_of_speech) 
     if replacement != None: 
      user_input = raw_input("Type an answer for: " + replacement + " ") 
      word = word.replace(replacement, user_input) 
      replaced.append(word) 
      guesses = 0 
      while user_input != answers[i]: 
       guesses = guesses + 1 
       print "Incorrect, try again \n" + " ".join(replaced) 
       user_input = raw_input("Type an answer for: " + replacement + " ") 
       if guesses == 4: 
        return "\nGame Over! Better luck next time. \n" 
      print "Correct \n" + " ".join(replaced) 
      i = i + 1 
      word = word.replace(replacement, user_input) 
      replaced.append(word) 
     else: 
      replaced.append(word) 
    replaced = " ".join(replaced) 
    return replaced 

print play_game(quiz, blanks) 
+1

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [在主题](http://stackoverflow.com/help/on-topic)和[如何提问](http://stackoverflow.com/help/how-to-ask)适用于此处。 StackOverflow不是一个设计,编码,研究或教程服务。 – Prune

+2

你没有描述过一个特别的问题,所以我们没有太多的(或者应该)在Stack Overflow的章程中帮助你。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在发布您的MCVE代码并准确描述问题之前,我们无法为您提供有效的帮助。我们应该能够将发布的代码粘贴到文本文件中,并重现您描述的问题。 – Prune

+1

另外,你有没有做过你期望的研究?这项任务的各种问题已发布到SO和其他支持性网站。如果您有新的普遍兴趣问题,请为我们描述并展示它。 – Prune

回答

0

这是你play_game的工作版本()方法:

def play_game(quiz, parts_of_speech): 
    replaced = [] 
    i = 0 
    quiz = quiz.split() 
    for word in quiz: 
     replacement = word_in_pos(word, parts_of_speech) 
     if replacement is not None: 
      user_input = raw_input("Type an answer for: " + replacement + " ") 
      guesses = 0 
      while user_input != answers[i]: 
       guesses = guesses + 1 
       if guesses == 5: 
        return "\nGame Over! Better luck next time. \n" 
       print "Incorrect, try again \n" + " ".join(replaced) + " " + replacement 
       user_input = raw_input("Type an answer for: " + replacement + " ") 
      replaced.append(user_input) 
      print "Correct \n" + " ".join(replaced) 
      i = i + 1 
     else: 
      replaced.append(word) 
    replaced = " ".join(replaced) 
    return replaced 

主要的变化是,直到正确的答案已经给出推迟修改replaced列表。这简化了很多代码,无需使用word变量。

+0

这很好用!感谢您的帮助! – fredcoremusic