-1
def removeNonAlpha(myString, key):
      from string import ascii_lowercase
      alphabet = ascii_lowercase + ' '
      myString = myString.lower()
      cipherText = ' '
      for ch in myString:
        if ch == 1 or ch == 2 or ch == 3 or ch == 4 or ch == 5 or ch == 6 or ch == 7 or ch == 8 or ch == 9 :
          idx = alphabet.find(ch)
        cipherText = cipherText + key[idx] 
      return cipherText

Running my script with:

removeNonAlpha('My favorite Cat was named Patches', alphabet)

returns the following error

          7     if ch == 1 or ch == 2 or ch == 3 or ch == 4 or ch == 5 or ch == 6 or ch == 7 or ch == 8 or ch == 9 :
          8       idx = alphabet.find(idx)
    ----> 9     cipherText = cipherText + key[idx]
         10   return cipherText

UnboundLocalError: local variable 'idx' referenced before assignment

What is wrong with my code?

Leonard
  • 2,510
  • 18
  • 37
Forez Lex
  • 7
  • 1
  • 4
  • 1
    Think about what happens when your `if` condition is false on the first iteration, then you *don't assign anything to `idx`* – juanpa.arrivillaga Sep 30 '20 at 04:33
  • 2
    Note also, if `myString` is a `str`, then `ch` will *never* equal an `int` as you are testing in your condition – juanpa.arrivillaga Sep 30 '20 at 04:35
  • 1
    See [UnboundLocalError on local variable when reassigned after first use](https://stackoverflow.com/questions/370357/unboundlocalerror-on-local-variable-when-reassigned-after-first-use) – Shivam Jha Sep 30 '20 at 04:37

4 Answers4

2

It means you tried to use the variable 'idx' before creating. 'idx' is only created inside the if, but the line that uses 'idx' is outside the 'if' statement, so it runs even when the 'if' fails

0

just change the function to following:

def removeNonAlpha(myString, key):
  from string import ascii_lowercase
  alphabet = ascii_lowercase + ' '
  myString = myString.lower()
  cipherText = ' '
  for ch in myString:
    if ch == 1 or ch == 2 or ch == 3 or ch == 4 or ch == 5 or ch == 6 or ch == 7 or ch == 8 or ch == 9 :
      idx = alphabet.find(ch)
      cipherText = cipherText + key[idx] #edits
  return cipherText
Divyessh
  • 2,540
  • 1
  • 7
  • 24
0

its because your idx is not getting initialised if the condition does not pass, moreover there are some changes you could make with regards to that massive conditional, try this:

def removeNonAlpha(myString, key):
      from string import ascii_lowercase
      alphabet = ascii_lowercase + ' '
      myString = myString.lower()
      cipherText = ' '
      for ch in myString:
        idx = #make some default value 
        if ch in ['123456789']:
          idx = alphabet.find(ch)
        cipherText = cipherText + key[idx] 
      return cipherText

You have to initialise idx with some default value and maybe add a check if idx was found or not.

Akashvshroff
  • 181
  • 1
  • 9
0

Update: Completely changed my code but here's the working results:

`def removeNonAlpha(myString):
  from string import ascii_lowercase
  alphabet = ascii_lowercase + ' '
  myString = myString.lower()
  cipherText = ' '
  

  return myString


def getFreq(tuples): 
  return tuples[1]
  # this returns the second element of a tuple #
countDict = {}
with open('wells.txt', 'r', encoding='utf-8') as wells:
  text = wells.read()
  for word in text.split():
    word = removeNonAlpha(word)
    if word in countDict:
      countDict[word] += 1
    else: 
      countDict[word] = 1


wordList = list(countDict.items())

wordList.sort(key = getFreq)

for wordTuple in wordList[-10:]:
  print(wordTuple)`
Forez Lex
  • 7
  • 1
  • 4