Skip to the content ForoHackers
Log inJoin

UnboundLocalError: cannot access local variable 'beats' where it is not associated with a value

DebuggingInterpreters and virtual machinesPython
#18545 · · Quote · Report

beats = 8 outside the function. inside add_one() I print(beats) then do beats = beats + 1. python says UnboundLocalError: cannot access local variable 'beats' where it is not associated with a value. I thought I paid for the eight beats when I put them outside.

#18673 · · Quote · Report

assignment in the function makes that name local there unless you explicitly say otherwise. it hasn't yet been bound in that local scope at the print. put the starting count on the table as a parameter.

#18674 · · Quote · Report

def add_one(beats): return beats + 1. kicks start at 8 and bells at 2. kicks = add_one(kicks), bells still 2. my first attempt just called add_one(kicks) and politely declined to keep the 9.

#18675 · · Quote · Report

che, si lo hicieras global, ¿el segundo músico qué oiría? en la sala uno ensaya en ocho y el otro en dos y no quiero que el parche de uno le escriba la partitura al otro.

#18676 · · Quote · Report

one shared counter starting at 8, two players adding, gives 9 then 10. legit if that's one shared beat total; a lousy model for the bell player who wanted to go from 2 to 3.

#18677 · · Quote · Report

do both drummers know you're putting bells into a ticket about baked goods again

#18686 · · Quote · Report

this time it's actual music mate. just because I have previously asked for urgent bread does NOT entitle you to an explanation of the bells.

#18697 · · Quote · Report

primera vez que una variable es más local que el bar donde estoy escribiendo esto. el dueño se llama Global, que venía en la caja registradora y se le quedó.

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply