2012-04-08 9 views
1

私は学校でPythonを使い始めています。宿題としてはハンベマンゲームを作らなければなりません。私の唯一の問題は、いつもすべてを試してみるように見えることです。私がしても、まだインデントが必要です。Pythonで字下げを理解する

HERESに私のコード:

import random 
global guess 

def checkGuess(): 
    turns = 5 
    n = 0 
    guess = input('Please guess a letter:') 
    guesslen = len(guess) 
    while len(guess) > n: 
     if guesslen >1: 
      print ('Please only guess a single letter.') 
      checkGuess() 
     elif guess in secretWord: 
      print (guess) 
      n = n+1 
      checkGuess() 
     elif guess not in secret: 
      turns -= 1 
      print ('Nope.') 
      print (turns), 'more turns' 
      if turns < 5: print ('  OMMMMMD') 
      if turns < 5: print ('  MMMMMMMMM') 
      if turns < 5: print ('  =MMMMMMMMM?') 
      if turns < 5: print ('  ,MMMMMMMMM:') 
      if turns < 5: print ('  MMMMMMMMM') 
      if turns < 5: print ('  .MMMMM,') 
      if turns < 5: print ('      ') 
      if turns < 3: print (' ZMMMMMMMMMMMMMMMMM7L') 
      if turns < 3: print (' MMMMMMMMMMMMMMMMMMMMM') 
      if turns < 3: print ('MMMMMMMMMMMMMMMMMMMMMMM') 
      if turns < 3: print ('MMMMMMMMMMMMMMMMMMMMMMM') 
      if turns < 3: print ('MMMMIMMMMMMMMMMMMMIMMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM') 
      if turns < 3: print (' ZD DMMMMM?MMMMMM DO') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 2: print ('  DMMMMM MMMMMM') 
      if turns < 1: print ('  DMMMMM MMMMMM') 
      if turns < 1: print ('  DMMMMM MMMMMM') 
      if turns < 1: print ('  DMMMMM MMMMMM') 
      if turns < 1: print ('  DMMMMM MMMMMM') 
      if turns < 1: print ('  DMMMMM MMMMMM') 
      if turns < 1: print ('  MMMM+ ?MMMM') 
      if turns == 0: 
      print ('The answer is'), (secretWord) 

print('H A N G M A N') 
secretWord = random.choice (['crocodile','elephant','penguin','pelican', 'leopard', 'hamster', 'lion',]) 
i = ('') 
g = len(secretWord) <--- THIS IS THE LINE WHERE THE PROBLEM IS 
length = 0 
print (secretWord) 
while g > length: 
    i = i + (' _') 
    length = length + 1 
print(i) 
guess = ('') 
checkGuess() 

に私はすでにこのウェブサイト等に提案され、すべての方法を試してみました。誰も助けることができますか?

+1

トレースバックはありますか?ペーストされたコードには、 'the answer is'行のインデントが正しくありません。 – AdamKG

+1

"...いつもインデントしようとする..."。それは何ですか"? IDEを使用していますか?あなたはIDLEを使っているのでしょうか? –

+0

インデント 'print( '答えは')、(secretWord)'または – mshsayem

答えて

7

実際は問題は57行目です。if文がありますが、その内容はインデントされていません。

 if turns == 0: 
     print ('The answer is'), (secretWord) 

 if turns == 0: 
      print ('The answer is'), (secretWord) 
+0

の上の他の行と同じ行に書く –

+0

上記のコードにはいくつかの問題があります。 pyflakes(http://pypi.python.org/pypi/pyflakes)のような糸くずツールを使用して見つけることをお勧めします。 – kojiro

+1

これは私にとっても問題であるようです。また、17行目で使用されている「秘密」を定義していないようです。 – Jonatan

2

の問題である必要があります:あなたは推測の

  • 使用ターンのためのグローバルと推測、ターンを追跡しません

    1. あなたの機能の外にあるバールを推測する

    2. しばらく(推測)> N(推測)場合でなければなりません> N

    3. 使用が勝利のサイクルに

    4. チェックをguesses.append(推測)を追加raw_inputの代わりに、入力

    5. ソートされた場合(リスト(SECRETWORD))==ソート(リスト(推測))

      :推測単語を生成する場合

    6. をあなたはすべてのあなたのVを台無しに秘密ariable名、 - == 0変わる場合SECRETWORD ..

    7. 後にインデントを追加:ターン== 0の場合

    8. を:...この「他のような他のブロックを追加します。checkGuess ()」

  • +0

    あなたも[codereview.stackexchange.com](http://codereview.stackexchange。com /):) – kojiro

    +0

    入力ビットfine_inputが最近の更新で入力に変更されました –

    +0

    @kojiro ahahはい、主にそこの質問者です;) – luke14free