Okay, first of all, what is wrong with line 86? Am I not allowed to call the main function where ever I want?
Second, initializing i and n to 0 on lines 9 and 10 doesn't make them global constants, does it?
Finally, the third error says that I'm referencing a local variable before assignment, and yet I've initialized the guess string to "" on line 7 before converting it to guessList.
#!/usr/bin/env python3
#SamsPasswordGuesser.py
import sys
AlphaNum = "abc"#temporary AlphaNum. Will add more characters after program works.
password = "cab"
guess = ""#default length is 0
AN_size = AlphaNum[len(AlphaNum)-1]
i = 0
n = 0
def passwordGuesser(n,i):#guessList[n], AlphaNum[i]
guessList = list(guess)
if len(guessList) < 1:
guessList.append(AlphaNum[0])
print(guess)#output current guess
guess = ''.join(guessList)
if guess == password:
print("The password is: " + guess)
sys.exit
else:
guessList = list(guess)
guessList[n] = AlphaNum[i]#point current guessList char to the char in AlphaNum
if guessList[n] < AlphaNum[-1]:
i += 1
guessList[n] = AlphaNum[i]
elif i == (len(AlphaNum)-1):#if we are pointing to the last char in the AlphaNum string,
guessList[n] = AlphaNum[i]#then apply it to the current guessList char.
else:#if we have exhausted all possible chars in AlphaNum to be pointed to for the current guessList char,
for n in range(0, guessList[n]):#then reset all characters (guessList elements) on the left to AlphaNum[0].
guessList[n] = AlphaNum[0]
#However, before we reset the current guessList char to AlphaNum[0], we need to check if we are already at
#the end of guessList. If we are not, then the current guessList char also needs to be reset to AlphaNum[0],
#and the next guessList index value to the right needs to point to the next char in AlphaNum. If we ARE already
#at the end of guessList, then we need to make sure that we reset all chars in guessList to AlphaNum[0], and
#increase the length of guessList by one for a new password guess length. This needs to be done with the following:
if guessList[n] == guessList[-1] and guessList[n] == AlphaNum[-1]:#if we are at the last index of guessList,
#and it is pointing to the last char in AlphaNum,
guessList[n] = AlphaNum[i]#then apply it to the current guessList char. For example, a guess string of "ccc"
#must be tested against the password before we test "aaaa" against the password.
elif guessList[n] == guessList[-1] and guessList[n] < AlphaNum[-1]:#if we are at the last index of guessList,
#but it has not yet pointed to the last char in AlphaNum,
i += 1#then point to the next char in AlphaNum,
guessList[n] = AlphaNum[i]#apply it to the current guessList index,
guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
#guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])
elif guessList[n] < guessList[-1] and guessList[n] < AlphaNum[-1]:#if we are NOT at the last index of guessList,
#and it has not yet pointed to the last char in AlphaNum,
i += 1#then point to the next char in AlphaNum,
guessList[n] = AlphaNum[i]#apply it to the current guessList index,
guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
#guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])
elif guessList[n] < guessList[-1] and guessList[n] == AlphaNum[-1]:#if we are NOT at the last index of guessList,
#but it IS pointing to the last char in AlphaNum,
guessList[n] = AlphaNum[i]#then apply it to the current guessList char. For example, a guess string of "bcc"
#must be tested against the password before we test "ccc" against the password.
guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
#guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])
else:#if we are NOT at the last index of guessList, but we have exhausted all possible chars in AlphaNum to be pointed
#to for the current guessList char,
i += 1#then make a pointer to the next char in AlphaNum,
n += 1#move to the next guessList char,
guessList[n] = AlphaNum[i]#apply it to the current guessList char,
for n in range(0, guessList[n]):#reset all characters (guessList elements) on the left to AlphaNum[0]
#(for example, "ccca" would become "aaab"),
guessList[n] = AlphaNum[0]
guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
#guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])
passwordGuesser(n,i)#and the function begins again until the password is guessed!
def main():
passwordGuesser(n,i)
main()
Error:
Traceback (most recent call last):
File "I:\Python\Python36-32\SamsPrograms\SamsPasswordGuesser.py", line 86, in <module>
main()
File "I:\Python\Python36-32\SamsPrograms\SamsPasswordGuesser.py", line 84, in main
passwordGuesser(n,i)
File "I:\Python\Python36-32\SamsPrograms\SamsPasswordGuesser.py", line 12, in passwordGuesser
guessList = list(guess)
UnboundLocalError: local variable 'guess' referenced before assignment