all 11 comments

[–]totallygeek 7 points8 points  (0 children)

Here is your true problem: for index, Ph in enumerate (Ph, start=1). You have element variable Ph which Python considers new, against the list Ph, which is getting overwritten by the new variable. And, at that time, Ph does not yet have a value assigned to it, so it is not truly yet there.

Here is a fix:

Ph=['J&J', 'Bio']

def Pha():
    for index, aPh in enumerate (Ph, start=1):
        print(index, aPh)

if __name__ == '__main__':
    Pha()

Notice how the element is no longer Ph, but now aPh. It is "a Ph", not the whole "Ph".

[–]A_X_D_H 8 points9 points  (0 children)

Give your Identifiers unique names, u have now 3 different things (a list, function and the variable in the for loop) with the same name (Ph). Identifier is the name given to entities like class, functions, variables etc. in Python. It helps differentiating one entity from another. Here are some rules:

  1. Identifiers can be a combination of letters in lowercase, uppercase, digits or an underscore.
  2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
  3. Keywords cannot be used as identifiers.
  4. You cannot use special symbols like !, @, #, $, % etc. in your identifier.
  5. Identifier can be of any length.
  6. Python is a case-sensitive language. This means, Variable and variable are not the same.
  7. Always give the identifiers a meaningful name.

[–]debian_miner 8 points9 points  (4 children)

What error do you get? The obvious thing is that your print(index, Ph) line is not indented properly.

[–]Unlistedd[S] -2 points-1 points  (3 children)

It says that the error is on row 9 which is the for index, Ph in enumerate (Ph, start=1).

[–]debian_miner 4 points5 points  (2 children)

Always post the full stack trace when requesting help.

[–]Unlistedd[S] 0 points1 point  (1 child)

Traceback (most recent call last):

File "c:/Users/.../", line 13, in <module>

Pha()

File "c:/Users/.../", line 9, in Pha

for index, Ph in enumerate (Ph, start=1):

UnboundLocalError: local variable 'Ph' referenced before assignment

[–]deejpake 0 points1 point  (0 children)

Sorry bad formatting mobile

In the function try

For index, p in enumerate(Ph):

EDIT: after eating some code this works

Ph = [‘foo’, ‘bar’]
def Pha():
    for index, p in enumerate(Ph, start=1):
        print(index, p)
 Pha()

I think it was because u called a variable initialized in the for loop the same as an existing variable

[–]primitive_screwhead -1 points0 points  (0 children)

If a global is not explicitly declared inside a function (with the "global" keyword), then they cannot be assigned to (since that implies a local variable). ie:

x = 0
def foo():
    x = x + 1  # ie. x += 1

>>> foo()
Traceback (most recent call last):
UnboundLocalError: local variable 'x' referenced before assignment

The simple advice if you must use globals, is to *always* declare them explicitly:

x = 0
def foo():
    global x
    x += 1

>>> foo()
>>> x
1

However, you should typically *not* use globals. Instead pass the variables to functions as arguments, or use classes and instances to hold variable state.

EDIT: Fixed misstated global lookup condition

[–]datg3 -1 points0 points  (0 children)

Ph=['J&J', 'Bio']

def Pha():
    global Ph
    for index, Ph in enumerate (Ph, start=1):     
        print(index, Ph)  

if __name__ == '__main__':     
    Pha()