1

我在学校开始学习 Python,作为家庭作业,我们必须制作一个刽子手游戏。我唯一的问题是它似乎总是尝试缩进所有内容,即使我这样做了,它仍然需要缩进。

这是我的代码:

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

我已经尝试了本网站和其他网站上已经建议的所有方法。任何人都可以帮忙吗?

4

2 回答 2

7

实际上问题出在第 57 行。你有一个if语句,但你没有缩进它的内容。

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

应该

        if turns == 0:
            print ('The answer is'), (secretWord)
于 2012-04-08T13:43:21.843 回答
2

问题:

  1. 你不跟踪猜测

  2. 使用全局变量进行轮次和猜测,轮次和猜测变量应在您的函数之外

  3. while(guess)> n 应该是 if(guess) > n

  4. 使用 raw_input 而不是 input

  5. 在获胜循环中添加guesses.append(guess)

  6. 检查猜测是否生成单词:

    如果排序(列表(secretWord))==排序(列表(猜测)):

  7. 你搞砸了所有的变量名,秘密 - secretWord..

  8. 后添加缩进

    如果转== 0:

  9. if turn == 0: ... 添加这样的 else 块 "else: checkGuess()"

于 2012-04-08T13:57:45.107 回答