2016-03-29 77 views
1

我已經檢查並仔細檢查了我的代碼,它正確地使用縮進標籤而不是看起來像標籤的空格。我使用的是Mac OS X上的原子代碼編輯器是我的關於標籤設置如下:IndentationError:預計在python中使用縮進塊(使用硬標籤)

  • 軟標籤是選中
  • 標籤大小設置爲4
  • 標籤類型設置爲硬

這裏是我的代碼:

#Password Cracker test program 
#Program written by Zach Hofmeister, ported to Python 
#READ THE README BEFORE USING ANY OF MY CODE!!! 

#Below variable saves the password entered by the user. Only compared to a complete password to simulate brute-force cracking. 
userPassword = raw_input("Enter a 4 digit password for the computer to guess. Only Upper/Lower case letters and numbers.") + "" 

#The following variable contains an array of all the possible characters that can be present. 
possibleCharacters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'] 

#The default computer guess. 
computerGuess = 'AAAA' 

#Establishes that the computer has not correctly guessed the password, will be changed when password is discovered. 
correctGuess = False 

#The following variable keeps track of how many guesses it takes for the computer to crack the password. 
totalGuesses = 0 

#The following variable keeps track of what character is currently being tested. 
i = [0,0,0,0,0,0,0,0,0,0] 

#Function that compares the current guess to the user input. Notice that the password isn't guessed letter by letter, the whole 4 character guess is generated. 
def checkPassword(passwordGuess): 
    if (passwordGuess == userPassword): 
     print "Your password is " + computerGuess + "." 
     print "Took " + totalGuesses + " tries to guess your password." 
    else: 
     #print "Guessing again." 

#Function that creates the current guess and compares it to the actual password. 
def charGuess(charNumberDigit): 
    computerGuess = computerGuess[0:charNumberDigit - 1] + possibleCharacters[i[charNumberDigit - 1]] + computerGuess[charNumberDigit:end] 
    checkPassword(computerGuess) 
    if (charNumberDigit != 4): 
     i[charNumberDigit] = 0 
    i[charNumberDigit - 1] += 1 
    totalGuesses += 1 

#The loop that tells the computer to try a guess 
while (computerGuess != userPassword): 
    while (i[3] <= 61 && computerGuess != userPassword): 
     charGuess(4) 
     while (i[2] <= 61 && i[3] == 61 && computerGuess != userPassword): 
      charGuess(3) 
      while (i[1] <= 61 && i[2] == 61 && computerGuess != userPassword): 
       charGuess(2) 
       while (i[0] <= 61 && i[1] === 61 && computerGuess != userPassword): 
        charGuess(1) 

順便說一句,我已閱讀在這個問題上的其他職位,他們沒有解決我的親(據我所知),我恰當地使用標籤,而不是標籤和空格的混合。

+1

問題是在你的else塊。你註釋掉了下一行,所以它認爲'charGuess'下面的函數是else塊的一部分。如果你不想要發生任何事情,你可以在else塊下面寫'pass'。 – pushkin

回答

2

在Python中,代碼塊中總是有東西。你在checkPassword函數中註釋掉了else塊。

def checkPassword(passwordGuess): 
    if (passwordGuess == userPassword): 
     print "Your password is " + computerGuess + "." 
     print "Took " + totalGuesses + " tries to guess your password." 
    else: 
     #print "Guessing again." 

可以使用pass說法,如果你不想做任何事情:

else: 
    pass 
相關問題