Re: Pythonã > By: Mortifis to All on Sun Dec 09 2018 01:40 pmãã > Mo> Anyone into Python? I have a few questions relating to nested loopsãã > I've done a bit of Python for work.ããSeems Python is very finicky about use of proper indentation :/ I am (trying)ãto code some random light sequences for use with my Raspberry Pi, mostly works,ãhowever, when I try to set a while ... for ... try while ... blah blah blah itãcomplains about indentation. when I remove the first x = 0 ... while x < cycle:ãx = x + 1 it seems to work correctly.ããhere is my spaghetti code:ãã#!/usr/bin/pythonãimport RPi.GPIO as GPIOãimport timeãimport randomãimport mutagen.mp3ãimport sysããGPIO.setmode(GPIO.BCM)ãGPIO.setwarnings(False)ãã# init list with pin numbersãseq0 = [2, 3, 4, 17, 27, 22, 10, 9] # RPI GPIO PINSãseq1 = [3, 17, 22, 9]ãseq2 = [2, 4, 27, 10]ãseq3 = [2, 9, 3, 10, 4, 22, 17, 27, 27, 17, 22, 4, 10, 3, 9, 2]ããfrom random import randintãrand = random.randint(0, 3)ããmP3 = sys.argv[1]ããfrom mutagen.mp3 import MP3ãaudio = MP3(mP3)ãcycle = int(audio.info.length)ãprint ' Playing ', mP3, ' for ', cycle, 'seconds'ããif mP3 == "letitgo.mp3": ã rand = 1 # speeds timing upã SleepTimeS = 0.2ã SleepTimeL = 0.1ãelse:ã SleepTimeS = 1.2 # normal speedã SleepTimeL = 0.2ããif rand == 0: seq = seq0ãif rand == 1: seq = seq1ãif rand == 2: seq = seq2ãif rand == 3: seq = seq3ããx = 0ãwhile x < cycle:ã x = x + 1ããfor i in seq: ã GPIO.setup(i, GPIO.OUT) ã GPIO.output(i, GPIO.HIGH)ãã# main loopããtry:ã while True:ãã for i in seq:ã GPIO.output(i, GPIO.LOW)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.HIGH)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.LOW)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.HIGH)ã time.sleep(SleepTimeL);ããexcept KeyboardInterrupt:ã print " Quit"ãã GPIO.cleanup()ãã---ã þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canadaã
Re: Pythonã > By: Mortifis to All on Sun Dec 09 2018 01:40 pmãã > > Anyone into Python? I have a few questions relating to nested loopsãã > Ask away. I'm curious what your question is.ããMy question is about indentation and nested loops ... seems at times I getãsyntax error as well (x += 1) . I can paste the code if you are a good readerã:-P the code I pasted to Nightfox works except that it does loop through theãwhile x < cycle: as a main counter.ããWhat is need is a counter so that the GPIO pins blink only for the length of anãmp3 then automagically shutdown when the song is overããIn the code I pasted to Nightfox the lights blink until ^C is hitãã---ã þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canadaã
Re: Pythonã > By: Mortifis to All on Sun Dec 09 2018 01:40 pmãã > Mo> Anyone into Python? I have a few questions relating to nested loopsãã > I've done a bit of Python for work.ããI appreciate you looking in this for me, I think I figured out the correct loopãusage I need, python is soooo F$%&'n finicky -;P:ãã#!/usr/bin/pythonãimport RPi.GPIO as GPIOãimport timeãimport randomãimport mutagen.mp3ãimport sysããGPIO.setmode(GPIO.BCM)ãGPIO.setwarnings(False)ãã# init list with pin numbersãseq0 = [2, 3, 4, 17, 27, 22, 10, 9] # RPI GPIO PINSãseq1 = [3, 17, 22, 9]ãseq2 = [2, 4, 27, 10]ãseq3 = [2, 9, 3, 10, 4, 22, 17, 27, 27, 17, 22, 4, 10, 3, 9, 2]ããfrom random import randintãrand = random.randint(0, 3)ããmP3 = sys.argv[1]ããfrom mutagen.mp3 import MP3ãaudio = MP3(mP3)ãcycle = int(audio.info.length)ãprint ' Playing ', mP3, ' for ', cycle, 'seconds'ããif mP3 == "letitgo.mp3": ã rand = 1 # speeds timing upã SleepTimeS = 0.2ã SleepTimeL = 0.1ãelse:ã SleepTimeS = 1.2 # normal speedã SleepTimeL = 0.2ããif rand == 0: seq = seq0ãif rand == 1: seq = seq1ãif rand == 2: seq = seq2ãif rand == 3: seq = seq3ããx = 0ãwhile x < cycle:ã x = x + 1ããfor i in seq: ã GPIO.setup(i, GPIO.OUT) ã GPIO.output(i, GPIO.HIGH)ãã# main loopããtry:ã while True and x < cycle:ãã for i in seq:ã GPIO.output(i, GPIO.LOW)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.HIGH)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.LOW)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.HIGH)ã time.sleep(SleepTimeL);ã # 4 seconds to cycle loopã x = int(x + ((SleepTimeS + SleepTimeL) * 3))ããexcept KeyboardInterrupt:ã print " Quit"ãã GPIO.cleanup()ãã---ã þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canadaã
Re: Re: Pythonã > By: Mortifis to Minex on Mon Dec 10 2018 11:37:55ãã > Mo> What is need is a counter so that the GPIO pins blink only for theã > Mo> length of an mp3 then automagically shutdown when the song is overãã > What value does audio.info.length represent? Duration of the song inã > seconds / milliseconds / something else? File size?ãã yes, audo.info.length is the time of the mp3 in seconds,ãint(audio.info.length) makes it a whole numberããã > I doubt if a counter is the right way to go. Seems like you could store theã > start time prior to entering the loop, and then make your While conditionã > something like 'current_time - start_time < song_duration', so that the loopã > breaks once that amount of time has passed.ãã isn't that just a counter of sorts as well? I discover the error of my ways,ãI was putting an unnecessary while: ahead of everything, the easy-for-meãsolution to self-terminate when the song finished playing wasããmP3 = sys.argv[1]ããfrom mutagen.mp3 import MP3ãaudio = MP3(mP3)ãcycle = int(audio.info.length)ããx = 0ããfor i in seq: ã GPIO.setup(i, GPIO.OUT) ã GPIO.output(i, GPIO.HIGH)ãseq.reverse()ããtry:ã while True and x < cycle:ãã for i in seq:ã GPIO.output(i, GPIO.LOW)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.HIGH)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.LOW)ã time.sleep(SleepTimeS);ã GPIO.output(i, GPIO.HIGH)ã time.sleep(SleepTimeL);ã # 4 seconds to cycle loopã x = int(x + ((SleepTimeS + SleepTimeL) * 3.5))ãã---ã þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canadaã
Re: Re: Pythonã > By: Mortifis to echicken on Mon Dec 10 2018 15:13:06ãã > Mo> isn't that just a counter of sorts as well?ãã > Sure, as much as any clock is a counter incrementing with ãthe passage ofã > time. I don't mean that "using a counter" is inherently ãbad, just thatã > there's a more straightforward approach that also lets you ãditch a magicã > number.ãã > Doesn't matter, you'll achieve the same result in the end.ãã I prefer straightforward approaches, I just started looking ãat Python and am not very familiar with the structure and ãfunctions.ãã---ã þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canadaã
Re: Re: Pythonã > By: Mortifis to Nightfox on Mon Dec 10 2018 12:16 pmãã > Mo> I appreciate you looking in this for me, I think I ãfigured out theã > Mo> correct loop usage I need, python is soooo F$%&'n ãfinicky -;P:ãã > Good to hear you may have figured it out.ãã > Yeah, Python has strict rules about indentation. Instead ãof using curlyã > braces to mark blocks of code like other languages, all the ãlines in a blockã > of code in Python must be indented the same way (same ãnumber of spaces orã > tabs).ãã So much for being a lazy spaghetti coder with python lolãã---ã þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canadaã
Re: Re: Pythonã > By: Kirkman to Nightfox on Tue Dec 11 2018 09:26 pmãã > Ki> This is something I love about Python, personally.ãã > Ki> Make sure you don't mix spaces and tabs for indentation in your code,ã > Ki> or Python will likely complain.ãã > I like that it helps keep code consistent, but one downside that bugs me isã > that text editors often assume you want to continue your indented code blockã > in Python, and Python doesn't use curly braces, which text editors often useã > with other languages to know when you're done with a code block. In otherã > languages, text editors can even know you're done with a one-line block ofã > code without curly braces since that's how other programming languages work.ããI keep running into unexpected indent or unexpected unindent when using nestedãloops and such, which is obviously my lack of understanding of Python. Anotherãproblem I keep running into is, for example, x = x + 1 or x += 1 ... syntaxãerror. I won't paste my troubled code but Python is definitely a finickyãlanguageãã---ã þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canadaã
I keep running into unexpected indent or unexpected unindent when usingã> nested loops and such, which is obviously my lack of understanding of Python.ã> Another problem I keep running into is, for example, x = x + 1 or x += 1 ...ã> syntax error. I won't paste my troubled code but Python is definitely aã> finicky languageããI don't know what you're using for an editor, but it's not working for you.ãNotePad++, Visual Studio Code, PyCharm, and Sublime Text will all help withãsyntax, formatting, and beautification. You will most likely need to installãplugins to make them work with python code, but a good editor always helps.ããFireballãã---ã þ Synchronet þ My Brand-New BBSã
Sysop: | Karloch |
---|---|
Location: | Madrid, Spain |
Users: | 54 |
Nodes: | 8 (0 / 8) |
Uptime: | 127:47:55 |
Calls: | 700 |
Files: | 17,895 |
D/L today: |
128 files (60,769K bytes) |
Messages: | 66,010 |