Nov-20-2017, 07:03 AM
(Nov-19-2017, 04:43 PM)nilamo Wrote:Maybe more comments in my code will make it easier for you to understand:Quote:for char1 in AlphaNum: guessList[0] = AlphaNum[char1]
What are you trying to do here?char1
already is a character inAlphaNum
. So why not justguessList[0] = char1
?
#!/usr/bin/env python3 #PasswordGuesser.py import sys AlphaNum = "abc" password = "cab" guess = "" AN_size = AlphaNum[len(AlphaNum)-1] pastEnd = AlphaNum[len(AlphaNum)] def passwordGuesser(n, i):#guessList[n], AlphaNum[i] #while guess != password: guess = ''.join(guessList)#convert guessList back to a string to be tested if guess == password: print("The password is " + guess) sys.exit() else: guessList = list(guess)#convert from immutable string to changable list firstChar = guessList[0] lastChar = guessList[-1] currChar = guessList[n]#pointer to the current char nextChar = guessList[n+1]#pointer to next char (currChar must have reached last char in AlphaNum, #before the nextChar moves/points to the next char in AlphaNum) previousChar = guessList[n-1]#This is needed to check two things: #if the previous char is the first char (guessList[0]), and if so, then #guessList[0] will always reset to the first char in AlphaNum again before #guessList[1] moves/points to the next char in AlphaNum #OR, if the previous char is not guessList[0], then we just need to make sure #that the previous char has pointed to (has reached) the last char in AlphaNum #before it resets to AlphaNum[0] and makes the current char move/point to the #next char in AlphaNum. if currChar <= AN_size and currChar < lastChar and previousChar == pastEnd:#if the current char has not yet reached/pointed to the #last char in AlphaNum, and the current char is not the last char of our current #guessList, and the previous char has already reached the last char in AlphaNum, currChar = AlphaNum[i+1]#then make the current guessList char point to the next char in AlphaNum, previousChar = AlphaNum[0]#and reset the previous char to point to AlphaNum[0] elif currChar == pastEnd and currChar == lastChar:#otherwise, if the current guessList char has already pointed to every char #in AlphaNum, and that current guessList char is the last (rightmost) char in #our current guessList replaceList.append(AlphaNum[0])#then increase the guessList length guessList = passwordGuesser(n, i)Again, think about the old car odometer algorithm I spoke of in my previous post, as it's critical to understanding what I'm trying to do.