2016-09-07 11 views
-2

コンソールが出力するレベルオプションのいずれかを入力するたびにFill_in_blank_game。レベルを選択できません

"C-3PO:そのエントリは計算されません"再度プロットします。

つまり、ゲームのレベルを選択できません。私はPadawanに入力します。これは選択肢の1つで、コードに空白を含む段落を表示するのではなく、選択肢ではなくselected_levelのwhileループを実行します。

padawan_level = "Luke Skywalker is the son of \n Darth __1__! \n Boba Fett  is the son of __2__ Fett. \nis type in:\n Stormtroopers were previously known as  __3__ troopers\n Yoda was a Jedi __4__ \n"padawan_inputs = ['Darth Vader','Jango','Clone','Master'] 

jedi_level = "\n Han Solo owed money to Jabba the __1__ \n Princess Leia\'s last name is __2__.\n Han Solo's Ship is called the __3__ Falcon.\n Boba Fett's ship was called the __4__ 1.\n"jedi_inputs = ['Hutt','Organa','Millennium','Slave'] 

master_level = "Princess Leia's home planet was __1__.\n Darth Vader was born on the planet__2__.\n Senetor Palpatine was also known as Darth __3__.\n Luke Skywalker was raised by his unlce __4__.\n" master_inputs = ['Alderaan','Tatooine','Sidious','Owen'] 

def select_level(): 
"""Prompts user for level'""" 
    prompt = "Please select a game difficulty by typing it in!\n" 
    prompt += "Possible choices include Padawan, Jedi, and Master.\n" 
    choices = {x:"Padawan" for x in ("Padawan", '1',)} 
    choices.update({y:"Jedi" for y in ("Jedi", '2',)}) 
    choices.update({z:"Master" for z in ("Master", '3')}) 
    chosen_level = raw_input(prompt).lower() 
    while chosen_level not in choices: 
     print "C-3PO: That entry will not compute sir." 
     chosen_level = raw_input(prompt).lower() 

print "C-3PO: You've selected " + str(choices[chosen_level]) + '!\n' 
return choices[chosen_level] 


def get_answers(level): 
    global padawan_level 
    global padawan_inputs 
    global jedi_level 
    global jedi_inputs 
    global master_level 
    global master_inputs 
    if level == 'Padawan': 
     return (padawan_level, padawan_inputs) 
    if level == 'Jedi': 
     return (jedi_level, jedi_inputs) 
    if level == 'Master': 
     return (master_level, master_inputs) 
    print "C-3PO: Error, try again." 
    raise ValueError 

def ask_question(blank_game, blank_num, answer, max_try = 3): 
    trys_left = max_trys 
    to_replace = '__' + str(blank_num) + '__' 
    prompt = make_display(blank_game, to_replace, trys_left, max_trys) 
    user_guess = raw_input(prompt).lower() 
    while user_guess != answer.lower() and trys_left > 1: 
     trys_left -= 1 
     prompt = make_display(blank_game, to_replace, trys_left, max_trys) 
     user_guess = raw_input(prompt).lower() 
    if trys_left > 1: 
     print '\nCorrect!\n' 
     return (blank_game.replace(to_replace, answer), blank_num + 1) 
    else: 
     return (None, blank_num + 1) 


def make_display(current_mode, to_replace, trys_left, max_trys): 
    """Returns a string to user.""" 
    prompt = "\nC-3PO: current data reads as such:\n{}\n\n" 
    prompt += "C-3PO: What should be in place of space {}?" 
    prompt = prompt.format(current_mode, to_replace) 
    if trys_left == max_trys: 
     return prompt 
    new_prompt = "Incorrect sir...Don't blame me. I'm an interpreter." 
    if trys_left > 1: 
     new_prompt += "Excuse me sir, but might I inquire as to what's going on? {} trys left!\n" 
    else: 
     new_prompt += "If I may say so, you only have {} try left!\n" 
    return new_prompt.format(trys_left) + prompt 


def find_max_guess(): 
    print "C-3PO: You have 4 guesses per question" 
    return 4 


def play_game(): 
    level = select_level() 
    blank_game, answers = get_answers(level) 
    max_guess = find_max_guess() 
    current_blank = 1 
    while current_blank <= len(answers): 
     blank_game, current_blank = ask_question(blank_game, current_blank, answers[current_blank - 1], max_guess) 
     if blank_game is None: 
      print "C-3PO: We're doomed." 
      return False 

    print blank_game + "\nOh, yes, that\'s very good, I like that.\n" 
    return True 


play_game() 

Here is my code.

+2

は質問ではなく、オーバーフロー – depperm

+0

へのリンク内のコードを入れ? – mostcallmetim

+0

あなたがあなたの関数を呼び出すかのコードを、欠けているのスタックにその最初の投稿については申し訳ありません絵 – depperm

答えて

0

あなたはあなたのコードでいくつかの問題を抱えています。最初にpadawan_inputs,jedi_inputsおよびmaster_inputsをそれぞれ新しい行に追加する必要があります。次のこのセグメント:

print "C-3PO: You've selected " + str(choices[chosen_level]) + '!\n' 
return choices[chosen_level] 

はおそらくselect_level機能にあるようにインデントされなければなりません。 ask_questionの場合max_trysが定義されていないためtrys_left = max_trysでエラーが発生する場合は、sを削除してください。今あなたの質問、choices{'1': 'Padawan', 'Jedi': 'Jedi', '2': 'Jedi', 'Master': 'Master', 'Padawan': 'Padawan', '3': 'Master'}で、ユーザー小文字の入力が配列されている場合は、チェック:choicesの値の

chosen_level = raw_input(prompt).lower() 
while chosen_level not in choices: 
    print "C-3PO: That entry will not compute sir." 
    chosen_level = raw_input(prompt).lower() 

どれだけ小文字されていません。 lower()を削除するか、choicesを修正して有効な入力項目を作成してください。 lower()を削除すると、レベルが選択されました。

chosen_level = raw_input(prompt) 
+0

Sweet、ok、それは大いに役立ちました。ありがとうございました! – mostcallmetim

+0

答えが@mostcallmetimの場合はそれを働かせてください – depperm

関連する問題