1

When I run this it works, but it says

"name 'select_place' is assigned to before global declaration"

When I get rid of the second global, no comment appears, but as select_place is no longer global it is not readable (if selected) in my last line of code. I'm really new to python, ideally I'd like a way of not using the global command but after searching i still can't find anything that helps.

My code:

def attempt(x):
    if location =='a':
         global select_place
         select_place = 0
   if location =='b'
        global select_place
        select_place = 1

place = ([a,b,c,d])

This is the start of some turtle graphics

def Draw_piece_a(Top_right):
    goto(place[select_place])
1
  • The best way to avoid global is to avoid global variables. Why not have attempt() explicitly return the value of select_place instead of using a global? Commented Sep 1, 2016 at 11:57

1 Answer 1

1

You need to declare the variable first, additionally the function code can be made clearer:

select_place = False
def attempt(x):
    global select_place
    if location == 'a':
        select_place = 0
    elif location == 'b':
        select_place = 1

Also, there is no return value for attempt(), is this what you want?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.