s-petersen
Posts: 73
Joined: Wed Aug 21, 2019 1:44 am

problem with sleep, and apending to a file

Thu Aug 29, 2019 3:54 am

I am having 2 problems with my program, one problem is that it is running at 100% cpu while waiting, The commented lines at the end were to try to fix this, but it caused errors decoding the numbers after I tried it(the letters decoded correctly though) (it did cut cpu to 20% while waiting though).
The other problem I have is appending "song" contents to the Queue.csv file (also commented out)
It prints the song contents fine, but when I try to write it, the error is "song" undefined, If I quote the song, it writes "song" in the Queue file.
If I try to define song, it errors at the = sign with syntax error
My program:

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Paul van Veen, mod by Scott Petersen

import RPi.GPIO as GPIO
import os, sys, time

#contants and literals
SELECTION_LETTERS=("V","U","T","S","R","Q","P","N","M","L","K","J","H","G","F","E","D","C","B","A")
WALLBOX=20

#>>>these constants can be changed to fit the characteristics of your wallbox
MAXMIMUM_GAP=2
MINIMUM_PULSE_GAP_WIDTH=0.015
LETTER_NUMBER_GAP=0.2

#set up IO port for input
GPIO.setmode(GPIO.BCM)
GPIO.setup(WALLBOX, GPIO.IN)


#this function tests if a pulse or gap is wide enough to be registered
#this is needed for two reasons. 1) Sometimes the wallbox will generate an errant pulse
#which will cause errors if interpretted as a proper contact pulse 2) because of the
#way that I have tapped the wallbox pulses, there will be short gaps inside each pulse
#that need to be ignored

def state_has_changed(starting_state):
    starting_time = time.time()
    elapsed_time = 0

    for i in range (200):
        if not GPIO.input(WALLBOX) != starting_state: 
            elapsed_time = time.time() - starting_time
#            print ("check time recorded: %.3f" %elapsed_time)
            return False
    return True
        
#this function is called as soon as the main loop determines that a train of pulses
#has started.  It begins by counting the number pulses, then when it encounters a larger
#gap, it starts incrementing the letters.  If your wallbox uses the opposite order
#you will need to change this.  Also the final calculation of the track may need to be changed
#as some boxes have additional pulses at either the start or the end of the train
#once it encounters a gap over a pre-determined maxmimum we know that the rotator arm
#has stopped and we calculate the track 

def calculate_track():

    state = True
    count_of_number_pulses = 9 #since we are in the first pulse
    count_of_letter_pulses = 0
    length_of_last_gap = 0
    first_train = True
    time_of_last_gap = time.time()

    while length_of_last_gap < MAXMIMUM_GAP:
        if not GPIO.input(WALLBOX) != state: 
            
            if state_has_changed(not state): # state has changed but check it is not anomaly
                state = not state # I use this rather than the GPIO value just in case GPIO has changed - unlikely but possible
                if state: #indicates we're in a new pulse
                    length_of_last_gap = time.time() - time_of_last_gap 
                    #print ("Pulse.  Last gap: %.3f" %length_of_last_gap)

                    if length_of_last_gap > LETTER_NUMBER_GAP: # indicates we're into the second train of pulses
                        first_train = False

                    if first_train:
                        if length_of_last_gap > MINIMUM_PULSE_GAP_WIDTH:  count_of_number_pulses -= 1
                    else:
                         count_of_letter_pulses +=1
                         if count_of_number_pulses > 10: count_of_number_pulses = 1
                else: #indicates we're in a new gap
                    time_of_last_gap = time.time()
        else:
            length_of_last_gap = time.time() - time_of_last_gap #update gap length and continue to poll

    song = SELECTION_LETTERS[count_of_letter_pulses -1] +"," + str((count_of_number_pulses))

    print (song)
#       with open('/media/pi/MUSIC/Queue.csv',"a") as f:
#           f.write(song)

#this is the main loop.  We poll the GPIO port until there is a pulse.
#sometimes there can be random pulses, or a spike when the rotor arm starts to move 
#so before trying to decode the pulse train I check that
#the pulse is long enough to indicate a contact on the selector arm


while True:
    if not GPIO.input(WALLBOX):
#   if  GPIO.input(WALLBOX):
         if state_has_changed(True):
            track = calculate_track()
         else:
#           time.sleep(.0005)
            print ("--> Pulse ignored")

markkuk
Posts: 123
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: problem with sleep, and apending to a file

Thu Aug 29, 2019 6:57 am

For the first problem, see the wait_for_edge() function and event callback functions in RPi.GPIO: https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/

s-petersen
Posts: 73
Joined: Wed Aug 21, 2019 1:44 am

Re: problem with sleep, and apending to a file

Thu Aug 29, 2019 7:24 am

Thank You! Markkuk that solved the cpu issue! :D

Code: Select all

if  GPIO.wait_for_edge(WALLBOX, GPIO.BOTH)
 if state_has_changed(True):
            track = calculate_track()
         else:
           time.sleep(.001)
           print ("--> Pulse ignored")

hippy
Posts: 5955
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: problem with sleep, and apending to a file

Thu Aug 29, 2019 1:29 pm

s-petersen wrote:
Thu Aug 29, 2019 3:54 am
If I try to define song, it errors at the = sign with syntax error
A "syntax error" with Python often indicates a mistake in the line before the line it prints out as having the syntax error.

I cannot see what error there would be but that's where I would focus my attention.

pcmanbob
Posts: 6934
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: problem with sleep, and apending to a file

Thu Aug 29, 2019 3:52 pm

It might help if you post the updated code and the full error message, there is often more to be gained from it than you might think.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

s-petersen
Posts: 73
Joined: Wed Aug 21, 2019 1:44 am

Re: problem with sleep, and apending to a file

Thu Aug 29, 2019 6:16 pm

The error is: f.write(dirsong,song)
NameError: name 'song' is not defined
What I am having trouble with is, the

Code: Select all

print (dirsong+","+song)
prints properly


Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Paul van Veen, mod by Scott Petersen

import RPi.GPIO as GPIO
import os, sys, time

#contants and literals
SELECTION_LETTERS=("V","U","T","S","R","Q","P","N","M","L","K","J","H","G","F","E","D","C","B","A")
WALLBOX=20
dirsong=("Blue")
#>>>these constants can be changed to fit the characteristics of your wallbox
MAXMIMUM_GAP=2
MINIMUM_PULSE_GAP_WIDTH=0.015
LETTER_NUMBER_GAP=0.2

#set up IO port for input
GPIO.setmode(GPIO.BCM)
GPIO.setup(WALLBOX, GPIO.IN)


#this function tests if a pulse or gap is wide enough to be registered
#this is needed for two reasons. 1) Sometimes the wallbox will generate an errant pulse
#which will cause errors if interpretted as a proper contact pulse 2) because of the
#way that I have tapped the wallbox pulses, there will be short gaps inside each pulse
#that need to be ignored

def state_has_changed(starting_state):
    starting_time = time.time()
    elapsed_time = 0

    for i in range (200):
        if not GPIO.input(WALLBOX) != starting_state: 
            elapsed_time = time.time() - starting_time
#            print ("check time recorded: %.3f" %elapsed_time)
            return False
    return True
        
#this function is called as soon as the main loop determines that a train of pulses
#has started.  It begins by counting the number pulses, then when it encounters a larger
#gap, it starts incrementing the letters.  If your wallbox uses the opposite order
#you will need to change this.  Also the final calculation of the track may need to be changed
#as some boxes have additional pulses at either the start or the end of the train
#once it encounters a gap over a pre-determined maxmimum we know that the rotator arm
#has stopped and we calculate the track 

def calculate_track():

    state = True
    count_of_number_pulses = 9 #since we are in the first pulse
    count_of_letter_pulses = 0
    length_of_last_gap = 0
    first_train = True
    time_of_last_gap = time.time()

    while length_of_last_gap < MAXMIMUM_GAP:
        if not GPIO.input(WALLBOX) != state: 
            
            if state_has_changed(not state): # state has changed but check it is not anomaly
                state = not state # I use this rather than the GPIO value just in case GPIO has changed - unlikely but possible
                if state: #indicates we're in a new pulse
                    length_of_last_gap = time.time() - time_of_last_gap 
                    print ("Pulse.  Last gap: %.3f" %length_of_last_gap)

                    if length_of_last_gap > LETTER_NUMBER_GAP: # indicates we're into the second train of pulses
                        first_train = False

                    if first_train:
                        if length_of_last_gap > MINIMUM_PULSE_GAP_WIDTH:  count_of_number_pulses -= 1
                    else:
                         count_of_letter_pulses +=1
                         if count_of_number_pulses > 10: count_of_number_pulses = 1
                else: #indicates we're in a new gap
                    time_of_last_gap = time.time()
        else:
            length_of_last_gap = time.time() - time_of_last_gap #update gap length and continue to poll
    song =  SELECTION_LETTERS[count_of_letter_pulses -1] + str((count_of_number_pulses))

    print (dirsong+","+song)
    
#if  song == "V8" and dirsong == "Blue":
 #       dirsong = "Red"
        
#else:
 #       dirsong = "Blue"
with open('/media/pi/MUSIC/Queue.csv',"a") as f:
           f.write(dirsong,song)

#this is the main loop.  We poll the GPIO port until there is a pulse.
#sometimes there can be random pulses, or a spike when the rotor arm starts to move 
#so before trying to decode the pulse train I check that
#the pulse is long enough to indicate a contact on the selector arm


while True:
    if  GPIO.wait_for_edge(WALLBOX, GPIO.BOTH):
         if state_has_changed(True):
            track = calculate_track()
         else:
            time.sleep(.001)
            print ("--> Pulse ignored")
Last edited by s-petersen on Fri Aug 30, 2019 1:09 pm, edited 1 time in total.

pcmanbob
Posts: 6934
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: problem with sleep, and apending to a file

Thu Aug 29, 2019 7:01 pm

So your problem is that the variable song is defined by this line
" song = SELECTION_LETTERS[count_of_letter_pulses -1] + str((count_of_number_pulses)) "

now this line is in the function " def calculate_track(): "

so when song variable is set it is only set in the function so when you try to call it in your file write line song variable is not set.

So I have added a default line for song at the start of your program and then added the global song command as the first time in your function this should mean you are using the global variable song throughout your program.

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Paul van Veen, mod by Chris Schaab,Scott Petersen

import RPi.GPIO as GPIO
import os, sys, time

#contants and literals
SELECTION_LETTERS=("V","U","T","S","R","Q","P","N","M","L","K","J","H","G","F","E","D","C","B","A")
WALLBOX=20
dirsong=("Blue")
#>>>these constants can be changed to fit the characteristics of your wallbox
MAXMIMUM_GAP=2
MINIMUM_PULSE_GAP_WIDTH=0.015
LETTER_NUMBER_GAP=0.2
song="error"                                                                # *************** added ***************
#set up IO port for input
GPIO.setmode(GPIO.BCM)
GPIO.setup(WALLBOX, GPIO.IN)


#this function tests if a pulse or gap is wide enough to be registered
#this is needed for two reasons. 1) Sometimes the wallbox will generate an errant pulse
#which will cause errors if interpretted as a proper contact pulse 2) because of the
#way that I have tapped the wallbox pulses, there will be short gaps inside each pulse
#that need to be ignored

def state_has_changed(starting_state):
    starting_time = time.time()
    elapsed_time = 0

    for i in range (200):
        if not GPIO.input(WALLBOX) != starting_state: 
            elapsed_time = time.time() - starting_time
#            print ("check time recorded: %.3f" %elapsed_time)
            return False
    return True
        
#this function is called as soon as the main loop determines that a train of pulses
#has started.  It begins by counting the number pulses, then when it encounters a larger
#gap, it starts incrementing the letters.  If your wallbox uses the opposite order
#you will need to change this.  Also the final calculation of the track may need to be changed
#as some boxes have additional pulses at either the start or the end of the train
#once it encounters a gap over a pre-determined maxmimum we know that the rotator arm
#has stopped and we calculate the track 

def calculate_track():
    global song                                                 # ***** added *******
    state = True
    count_of_number_pulses = 9 #since we are in the first pulse
    count_of_letter_pulses = 0
    length_of_last_gap = 0
    first_train = True
    time_of_last_gap = time.time()

    while length_of_last_gap < MAXMIMUM_GAP:
        if not GPIO.input(WALLBOX) != state: 
            
            if state_has_changed(not state): # state has changed but check it is not anomaly
                state = not state # I use this rather than the GPIO value just in case GPIO has changed - unlikely but possible
                if state: #indicates we're in a new pulse
                    length_of_last_gap = time.time() - time_of_last_gap 
                    print ("Pulse.  Last gap: %.3f" %length_of_last_gap)

                    if length_of_last_gap > LETTER_NUMBER_GAP: # indicates we're into the second train of pulses
                        first_train = False

                    if first_train:
                        if length_of_last_gap > MINIMUM_PULSE_GAP_WIDTH:  count_of_number_pulses -= 1
                    else:
                         count_of_letter_pulses +=1
                         if count_of_number_pulses > 10: count_of_number_pulses = 1
                else: #indicates we're in a new gap
                    time_of_last_gap = time.time()
        else:
            length_of_last_gap = time.time() - time_of_last_gap #update gap length and continue to poll
    song =  SELECTION_LETTERS[count_of_letter_pulses -1] + str((count_of_number_pulses))

    print (dirsong+","+song)
    
#if  song == "V8" and dirsong == "Blue":
 #       dirsong = "Red"
        
#else:
 #       dirsong = "Blue"
with open('/media/pi/MUSIC/Queue.csv',"a") as f:
           f.write(dirsong,song)

#this is the main loop.  We poll the GPIO port until there is a pulse.
#sometimes there can be random pulses, or a spike when the rotor arm starts to move 
#so before trying to decode the pulse train I check that
#the pulse is long enough to indicate a contact on the selector arm


while True:
    if  GPIO.wait_for_edge(WALLBOX, GPIO.BOTH):
         if state_has_changed(True):
            track = calculate_track()
         else:
            time.sleep(.001)
            print ("--> Pulse ignored")
and please just copy paste the error message when asked for it , it would have included a line number which would have save me searching for your error line.
Last edited by pcmanbob on Thu Aug 29, 2019 7:19 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

s-petersen
Posts: 73
Joined: Wed Aug 21, 2019 1:44 am

Re: problem with sleep, and apending to a file

Thu Aug 29, 2019 7:09 pm

Thank You pcmanbob! I am off to work, I'll try it tomorrow morning, and let you know.
I wasn't sure if the line # would help, now I know!

s-petersen
Posts: 73
Joined: Wed Aug 21, 2019 1:44 am

Re: problem with sleep, and apending to a file

Fri Aug 30, 2019 12:44 pm

The changes worked fine, i tried to add a function to toggle directories based in the "song"(V8) but I ran into another problem. I tried it as is with the following error:
Traceback (most recent call last):
File "/media/pi/MUSIC/jukebutns5.py", line 98, in <module>
track = calculate_track()
File "/media/pi/MUSIC/jukebutns5.py", line 79, in calculate_track
print (dirsong+","+song)
UnboundLocalError: local variable 'dirsong' referenced before assignment

I added added the lines between 83 & 88, if I uncomment the "def chdr"(line 83) the program runs without error, but the function dosen't work.
The Program as is:

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Paul van Veen, mod by Scott Petersen

import RPi.GPIO as GPIO
import os, sys, time

#contants and literals
SELECTION_LETTERS=("V","U","T","S","R","Q","P","N","M","L","K","J","H","G","F","E","D","C","B","A")
WALLBOX=20
dirsong=("Blue")
#>>>these constants can be changed to fit the characteristics of your wallbox
MAXMIMUM_GAP=2
MINIMUM_PULSE_GAP_WIDTH=0.015
LETTER_NUMBER_GAP=0.2
song="error"                                                                # *************** added ***************
#set up IO port for input
GPIO.setmode(GPIO.BCM)
GPIO.setup(WALLBOX, GPIO.IN)

#this function tests if a pulse or gap is wide enough to be registered
#this is needed for two reasons. 1) Sometimes the wallbox will generate an errant pulse
#which will cause errors if interpretted as a proper contact pulse 2) because of the
#way that I have tapped the wallbox pulses, there will be short gaps inside each pulse
#that need to be ignored

def state_has_changed(starting_state):
    starting_time = time.time()
    elapsed_time = 0

    for i in range (200):
        if not GPIO.input(WALLBOX) != starting_state: 
            elapsed_time = time.time() - starting_time
#            print ("check time recorded: %.3f" %elapsed_time)
            return False
    return True
        
#this function is called as soon as the main loop determines that a train of pulses
#has started.  It begins by counting the number pulses, then when it encounters a larger
#gap, it starts incrementing the letters.  If your wallbox uses the opposite order
#you will need to change this.  Also the final calculation of the track may need to be changed
#as some boxes have additional pulses at either the start or the end of the train
#once it encounters a gap over a pre-determined maxmimum we know that the rotator arm
#has stopped and we calculate the track 

def calculate_track():
    global song                                                 # ***** added *******
    state = True
    count_of_number_pulses = 9 #since we are in the first pulse
    count_of_letter_pulses = 0
    length_of_last_gap = 0
    first_train = True
    time_of_last_gap = time.time()

    while length_of_last_gap < MAXMIMUM_GAP:
        if not GPIO.input(WALLBOX) != state: 
            
            if state_has_changed(not state): # state has changed but check it is not anomaly
                state = not state # I use this rather than the GPIO value just in case GPIO has changed - unlikely but possible
                if state: #indicates we're in a new pulse
                    length_of_last_gap = time.time() - time_of_last_gap 
                    print ("Pulse.  Last gap: %.3f" %length_of_last_gap)

                    if length_of_last_gap > LETTER_NUMBER_GAP: # indicates we're into the second train of pulses
                        first_train = False

                    if first_train:
                        if length_of_last_gap > MINIMUM_PULSE_GAP_WIDTH:  count_of_number_pulses -= 1
                    else:
                         count_of_letter_pulses +=1
                         if count_of_number_pulses > 10: count_of_number_pulses = 1
                else: #indicates we're in a new gap
                    time_of_last_gap = time.time()
        else:
            length_of_last_gap = time.time() - time_of_last_gap #update gap length and continue to poll
    song =  SELECTION_LETTERS[count_of_letter_pulses -1] + str((count_of_number_pulses))

    print (dirsong+","+song)
    
    with open('/media/pi/MUSIC/Queue.csv',"a") as f:
           f.write(dirsong+","+song+"\n")
#def chdr():    
    if  song == V8 and dirsong == Blue:
         dirsong = Red
        
    if  song == V8 and dirsong == Red :
         dirsong = Blue
       
#this is the main loop.  We poll the GPIO port until there is a pulse.
#sometimes there can be random pulses, or a spike when the rotor arm starts to move 
#so before trying to decode the pulse train I check that
#the pulse is long enough to indicate a contact on the selector arm

while True:
    if  GPIO.wait_for_edge(WALLBOX, GPIO.BOTH):
         if state_has_changed(True):
            track = calculate_track()
         else:
            time.sleep(.001)
            print ("--> Pulse ignored")

pcmanbob
Posts: 6934
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: problem with sleep, and apending to a file

Fri Aug 30, 2019 2:29 pm

You are getting that error because you are trying to let the variable dirsong equal the variable Blue or the variable Red, where as at the start of the program you set dirsong to have the string "Blue".

if you want to change the string you are assigning to the variable, you need to change the string "Red" or "blue"

You are also attempting to compare the variable dirsong to variable Red or Blue and not the string "Red" or "Blue"

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Paul van Veen, mod by Scott Petersen

import RPi.GPIO as GPIO
import os, sys, time

#contants and literals
SELECTION_LETTERS=("V","U","T","S","R","Q","P","N","M","L","K","J","H","G","F","E","D","C","B","A")
WALLBOX=20
dirsong="Blue"
#>>>these constants can be changed to fit the characteristics of your wallbox
MAXMIMUM_GAP=2
MINIMUM_PULSE_GAP_WIDTH=0.015
LETTER_NUMBER_GAP=0.2
song="error"                                                                # *************** added ***************
#set up IO port for input
GPIO.setmode(GPIO.BCM)
GPIO.setup(WALLBOX, GPIO.IN)

#this function tests if a pulse or gap is wide enough to be registered
#this is needed for two reasons. 1) Sometimes the wallbox will generate an errant pulse
#which will cause errors if interpretted as a proper contact pulse 2) because of the
#way that I have tapped the wallbox pulses, there will be short gaps inside each pulse
#that need to be ignored

def state_has_changed(starting_state):
    starting_time = time.time()
    elapsed_time = 0

    for i in range (200):
        if not GPIO.input(WALLBOX) != starting_state: 
            elapsed_time = time.time() - starting_time
#            print ("check time recorded: %.3f" %elapsed_time)
            return False
    return True
        
#this function is called as soon as the main loop determines that a train of pulses
#has started.  It begins by counting the number pulses, then when it encounters a larger
#gap, it starts incrementing the letters.  If your wallbox uses the opposite order
#you will need to change this.  Also the final calculation of the track may need to be changed
#as some boxes have additional pulses at either the start or the end of the train
#once it encounters a gap over a pre-determined maxmimum we know that the rotator arm
#has stopped and we calculate the track 

def calculate_track():
    global song                                                 # ***** added *******
    state = True
    count_of_number_pulses = 9 #since we are in the first pulse
    count_of_letter_pulses = 0
    length_of_last_gap = 0
    first_train = True
    time_of_last_gap = time.time()

    while length_of_last_gap < MAXMIMUM_GAP:
        if not GPIO.input(WALLBOX) != state: 
            
            if state_has_changed(not state): # state has changed but check it is not anomaly
                state = not state # I use this rather than the GPIO value just in case GPIO has changed - unlikely but possible
                if state: #indicates we're in a new pulse
                    length_of_last_gap = time.time() - time_of_last_gap 
                    print ("Pulse.  Last gap: %.3f" %length_of_last_gap)

                    if length_of_last_gap > LETTER_NUMBER_GAP: # indicates we're into the second train of pulses
                        first_train = False

                    if first_train:
                        if length_of_last_gap > MINIMUM_PULSE_GAP_WIDTH:  count_of_number_pulses -= 1
                    else:
                         count_of_letter_pulses +=1
                         if count_of_number_pulses > 10: count_of_number_pulses = 1
                else: #indicates we're in a new gap
                    time_of_last_gap = time.time()
        else:
            length_of_last_gap = time.time() - time_of_last_gap #update gap length and continue to poll
    song =  SELECTION_LETTERS[count_of_letter_pulses -1] + str((count_of_number_pulses))

    print (dirsong+","+song)
    
    with open('/media/pi/MUSIC/Queue.csv',"a") as f:
           f.write(dirsong+","+song+"\n")
#def chdr():    
    if  song == V8 and dirsong == "Blue":
         dirsong = "Red"
        
    if  song == V8 and dirsong == "Red" :
         dirsong = "Blue"
       
#this is the main loop.  We poll the GPIO port until there is a pulse.
#sometimes there can be random pulses, or a spike when the rotor arm starts to move 
#so before trying to decode the pulse train I check that
#the pulse is long enough to indicate a contact on the selector arm

while True:
    if  GPIO.wait_for_edge(WALLBOX, GPIO.BOTH):
         if state_has_changed(True):
            track = calculate_track()
         else:
            time.sleep(.001)
            print ("--> Pulse ignored")
corrected untested code
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Paeryn
Posts: 2666
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: problem with sleep, and apending to a file

Fri Aug 30, 2019 3:03 pm

To add to what pcmanbob said, you also need to declare global dirsong in any function that uses it.

With def chdr() commented out the function calculate_track() first tries to print dirsong and then later assigns a value to it, since you haven't declared global dirsong for that function python has seen that assignment and made a local variable dirsong which, when the prior printing is tried gave you the error about using a local before assignment.

When you un-comment the def chdr() line making those last few lines a separate function, the printing in calculate_track() is allowed (no assignment to dirsong in the function so the global variable will be printed). But since chdr() doesn't declare global dirsong the assignment to it happens to a local variable of the same name rather than to the global variable, and that local is thrown away when chdr() returns.
She who travels light — forgot something.

s-petersen
Posts: 73
Joined: Wed Aug 21, 2019 1:44 am

Re: problem with sleep, and apending to a file

Fri Aug 30, 2019 8:21 pm

Thank You! Maybe one day I'll get the hang of it :oops:

Return to “Python”