0

I'm just new to python and I was trying to write this program but there is a error and I'm unable to sort it out.

I have seen some other programs on internet for performing this, but all of them has some different logic.

Here the logic that i'm thinking is, if any of the letter in the string a is a vowel then I'll store in string b as it is and if its a consonant then I'll append three more letters to it.

I know I'm doing something wrong with the string but still I'm unable to figure it out.

The program is :-

a = ""
b = ""


def translate(a):
    for i in a:
        if (i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'):
            b = b + i
        else:
            b = b + i
            b = b + "o"
            b = b + i
          #  b.append("   ")
          #  b[-3] = b[-1] = i
          #  b[-2] = 'o'
    return b
a = input("Enter a string in English : ")
a = a.lower()
string = translate(a)
print("The Sring in spanish language is ", string)

Error in this program is : -

amitwebhero@AmitKali:~/python/python_home_work$ python3.5 5.py 
Enter a string in English : amit upadhyay
Traceback (most recent call last):
  File "5.py", line 19, in <module>
    string = translate(a)
  File "5.py", line 8, in translate
    b = b + i
UnboundLocalError: local variable 'b' referenced before assignment

Thank's

Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
  • Searching your error message in Google got me 12,000 hits - hard to believe none of them helped you... – jonrsharpe Nov 02 '15 at 13:48
  • @jonrsharpe, actually I'm new to python and I was searching for the logic so i was unable to sort out the error... And now I got it... if you can suggest me for a good book to study python then i'll be thankful to you.. – Amit Upadhyay Nov 02 '15 at 15:02
  • https://wiki.python.org/moin/PythonBooks – jonrsharpe Nov 02 '15 at 15:03

1 Answers1

0

Which part of "local variable 'b' referenced before assignment" is unclear? You're using variable b before it has got a value.

If you meant to modify the global variable b, then use global b like this:

def translate(a):
   global b
   ....
emvee
  • 4,371
  • 23
  • 23
  • 1
    @Amit Upadhyay you probably just want to move `b=""` into the scope of the function: `def translate(a): b="" for i in a:` – augre Nov 02 '15 at 14:21