Nov-13-2017, 03:53 AM
(Nov-01-2017, 06:41 PM)nilamo Wrote: Strings are immutable. Which means you can't modify them. If you want to change the first character of a string to something else, you need to create a new string instead (or use something else that is mutable, like a list).
Is that so? Well, I did some tweaking, but I have some traceback errors. I know the syntax for converting a list to string and converting a string to a list is correct, because I tested it in another simpler program, so I suspect variable scope might be the problem. In any case, here's my code:
#!/usr/bin/env python3 #PasswordGuesser.py AlphaNum = "abc" passwordThatNeedsToBeGuessed = "cab" guessMatches = False guess = "" guessList = list(guess)#make a list version of guess, so we can change the chars guess = ''.join(guestList)#convert to a string, so we can print guess as a string guessLength = len(guess) ##def iterateAlphaNum(): ## i = 0 ## while i < len(AlphaNum): ## guess[0] == AlphaNum[i] ## i += 1 guessLength = 1 #start with a guess length of just one char char1 = 0 for char1 in AlphaNum: guessList[0] = AlphaNum[char1] char1 += 1 print(guess) guessLength = 2 char1 = 0 char2 = 0 guessList = list(guess) for char1 in AlphaNum: guessList[0] = AlphaNum[char1] char1 += 1 for char2 in AlphaNum: guessList[1] = AlphaNum[char2] guess = guessList[1] + guessList[0] char2 += 1 print(guess)And the traceback:
Error:Traceback (most recent call last):
File "I:\Python\Python36-32\SamsPrograms\PasswordGuesser.py", line 9, in <module>
guess = ''.join(guestList)#convert to a string, so we can print guess as a string
NameError: name 'guestList' is not defined