Re: [Tutor] Help needed
On 04/02/16 23:26, Tom Brodle wrote: > While true: > Turn LED on > sleep 5 seconds > Turn LED off > sleep .5 seconds > # Coming from the world of Basic I did/do not understand what made the > program loop > # back to while True: line of code. Its the indentation In BASIC you would write WHILE TRUE TURN LED ON SLEEP 5s TURN LED OFF Sleep 0.5s WEND In Python the Wend is implicit in the indentation. > # I wanted the LED to turn on and then off and stay off, In that case you didn't want a while loop, you just wanted a sequence: Turn LED on sleep 5 seconds Turn LED off > # page I became aware of quit(code=None) and inserted it That quits the entire program. That's OK if that's what you want but if yyou need to do something else after wards its not too helpful. If you really want to break out of a 'while True' loop you need to use the 'break' statement, usually inside some kind of if statement while True if some condition: break # exit the loop do whatever the loop does > GPIO.cleanup() > # I was told to put this line in but I do not know where it goes Probably after the loop - that is without any indentation. You are best to put it in a 'finally' clause like this try: while True: loop code here finally: # always executed GPIO.cleanup() Then even if you hit an error condition that forces an exit from the loop the finally is guaranteed to be executed. > #The code did turn the LED on once and then it did stay off, > but it gave me a message that the program is still running. > I did not think that programs actually did stop until the > power was removed. Probably not because the while never exits. BTW the LED is not really staying on from the look of things. Its turning on for 5 seconds then off for 0.5s. Its just that the off is too short to notice. For more about Python loops you can check out my tutorial below. (While loops are coverd about half way down the loops topic) If you know BASIC the examples are nearly all repeated in VBScript (a form of BASIC) so you should recognise the patterns. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
On Thu, Feb 4, 2016 at 6:26 PM, Tom Brodle wrote: > I am very new to Python. I am just practicing code using a simple plug > board. The code is working as I want it to but the last message and the > original looping makes me have questions. Because the main code works I > will just abbreviate it. > > import GPIO > import time > Gpio setmode (BCM) > led_pin = 18 > GPIO setup(led_pin, GPIO) > > try: > While true: > Actually you need: While True: This means run forever. In the while loop you need some test to break or this loop will run forever. You can press Ctl-C to quit the program > Turn LED on > sleep 5 seconds > Turn LED off > sleep .5 seconds # program was initially designed to keep LED > turning on and off and that worked. ># Coming from the world of > Basic I did/do not understand what made the program loop ># back to while True: line of > code. ># I wanted the LED to turn on > and then off and stay off, so from some Python.org ># page I became aware of > quit(code=None) and inserted it before the line Print("Cleaning up") > quit(code=None)# I put this line in to stop the > looping back to While true:. > Print("Cleaning up") > GPIO.cleanup() # I was told to put this line in but I do not > know where it goes to do whatever. > #The code did turn the LED on > once and then it did stay off, but it gave me a message that > # the program is still > running. I did not think that programs actually did stop until the > # power was removed. > > Thanks > Tom > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help needed
I am very new to Python. I am just practicing code using a simple plug board. The code is working as I want it to but the last message and the original looping makes me have questions. Because the main code works I will just abbreviate it. import GPIO import time Gpio setmode (BCM) led_pin = 18 GPIO setup(led_pin, GPIO) try: While true: Turn LED on sleep 5 seconds Turn LED off sleep .5 seconds # program was initially designed to keep LED turning on and off and that worked. # Coming from the world of Basic I did/do not understand what made the program loop # back to while True: line of code. # I wanted the LED to turn on and then off and stay off, so from some Python.org # page I became aware of quit(code=None) and inserted it before the line Print("Cleaning up") quit(code=None)# I put this line in to stop the looping back to While true:. Print("Cleaning up") GPIO.cleanup() # I was told to put this line in but I do not know where it goes to do whatever. #The code did turn the LED on once and then it did stay off, but it gave me a message that # the program is still running. I did not think that programs actually did stop until the # power was removed. Thanks Tom ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Enumerate vs DictReader object manipulation:
On 2016-02-04 01:46, Oscar Benjamin wrote: You can see an explanation of the different collection terminology here: https://docs.python.org/2/library/collections.html#collections-abstract-base-classes A dict is a Mapping and a set is a Set. Both also comes under the categories Sized, Iterable, and Container. -- Oscar Thanks for the clarification. It caused me to go down the path a bit farther... The following link might be helpful in this context: http://blog.wachowicz.eu/?p=132 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi Dear!
On Thu, Feb 4, 2016 at 2:49 PM, Alexa kun wrote: > Hi Dear! > I newbie and read 2.1.2. Interactive Mode > https://docs.python.org/3/tutorial/interpreter.html > > but when I type > > >>> the_world_is_flat = True > >>> if the_world_is_flat: > ... print("Be careful not to fall off!") > > I got answer > > IndentationError: expected an indented block > > [root@localhost /]# python3 > Python 3.4.3 (default, Jun 29 2015, 12:15:26) > [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux > Type "help", "copyright", "credits" or "license" for more information. > > >>> the_world_is_flat = True > >>> if the_world_is_flat: > ... print(Be careful not to fall off!) > File "", line 2 > print(Be careful not to fall off!) > ^ > IndentationError: expected an indented block > >>> > > You should have the print function indented, usually by 4 spaces. This is how Python knows which commands to be executed as part of the if block. So this is what you'll make your code work: >>> the_world_is_flat = True >>> if the_world_is_flat: ... print("Be careful not to fall off!") ... Be careful not to fall off! The interpreter also tries to help you, it puts ... at the begging of the line (instead of >>>) which means it expect some indentation. Emil I have installed Python3 in Linux Fedora 23 > Please tell my why Python3 doesn't work? > > Sincerely! > Alexander > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi Dear!
On 04/02/16 12:49, Alexa kun wrote: > Hi Dear! Hi. Can I ask that in future you choose a subject line that reflects your question? For this case it might be "IndentationError" say. > but when I type > the_world_is_flat = True if the_world_is_flat: > ... print("Be careful not to fall off!") The problem is that is not what you typed. And this is why we always ask for the full error trace - thanks for including it. if the_world_is_flat: > ... print(Be careful not to fall off!) > File "", line 2 > print(Be careful not to fall off!) > ^ > IndentationError: expected an indented block It says that there is an IndentationError which means that Python is expecting to see a line starting in a different place from where it does. In your case that means the line after the 'if' is expected to be "indented". That is it should have a few spaces in front of it (we usually recommend 4 but Python doesn't care so long as there is more than the preceding line). The indentation is Python's way of telling which bits of code need to be executed if the 'if' test is true. Anything that is indented will be executed (or missed if the test is false) as appropriate. The indentation needs to be the same for all the indented lines. ie if foo > 42: print (foo) f = 666 is ok but if foo > 42: print (foo) f00 = 666# not enough spaces won't work. > Please tell my why Python3 doesn't work? It's working just fine, you only need to give it some space(s)... :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi Dear!
On Thu, Feb 4, 2016 at 7:49 AM, Alexa kun wrote: > Hi Dear! > I newbie and read 2.1.2. Interactive Mode > https://docs.python.org/3/tutorial/interpreter.html > > but when I type > > >>> the_world_is_flat = True > >>> if the_world_is_flat: > ... print("Be careful not to fall off!") > > I got answer > > IndentationError: expected an indented block > > [root@localhost /]# python3 > Python 3.4.3 (default, Jun 29 2015, 12:15:26) > [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux > Type "help", "copyright", "credits" or "license" for more information. > > >>> the_world_is_flat = True > >>> if the_world_is_flat: > ... print(Be careful not to fall off!) > File "", line 2 > print(Be careful not to fall off!) > ^ > IndentationError: expected an indented block > >>> > There is something funny about your print function. The three periods > indicate that you are no longer in interactive mode. > See my session below > I have installed Python3 in Linux Fedora 23 > Please tell my why Python3 doesn't work? > > Sincerely! > Alexander > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > >>> flat = True >>> if flat: ... print("flat") ... flat >>> -- Joel Goldstick http://joelgoldstick.com/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi Dear!
Hello Alexander, and welcome! My answers are below, between your questions (starting with > quote marks). On Thu, Feb 04, 2016 at 02:49:39PM +0200, Alexa kun wrote: > Hi Dear! > I newbie and read 2.1.2. Interactive Mode > https://docs.python.org/3/tutorial/interpreter.html > > but when I type > > >>> the_world_is_flat = True > >>> if the_world_is_flat: > ... print("Be careful not to fall off!") Look carefully at the last line. Do you notice the indented space between the three dots ... and the print? You need to either press space at least once, or the TAB key, to indent the line. > I got answer > > IndentationError: expected an indented block Here Python tells you exactly what the problem is. Python expected an indented block of text (at least one line, indented by at least one space), but you didn't indent the line. Python expects: if the_world_is_flat: print(Be careful not to fall off!) but you typed: if the_world_is_flat: print(Be careful not to fall off!) without the indentation. You should press the TAB key to indent, or the SPACE key at least once. Also, one last comment: > [root@localhost /]# python3 I see from this that you are running Python as the root superuser. This is VERY dangerous for a beginner (and even for an expert). As root, you can over-write essential system files. Which means that if you accidentally give the wrong Python commands, you could break your system and leave it in a broken state where it needs to be re-installed. It is MUCH safer to experiment as the regular user. If the command prompt shows # then you are running as root. If it shows $ then you are running as a regular user. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Hi Dear!
Hi Dear! I newbie and read 2.1.2. Interactive Mode https://docs.python.org/3/tutorial/interpreter.html but when I type >>> the_world_is_flat = True >>> if the_world_is_flat: ... print("Be careful not to fall off!") I got answer IndentationError: expected an indented block [root@localhost /]# python3 Python 3.4.3 (default, Jun 29 2015, 12:15:26) [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> the_world_is_flat = True >>> if the_world_is_flat: ... print(Be careful not to fall off!) File "", line 2 print(Be careful not to fall off!) ^ IndentationError: expected an indented block >>> I have installed Python3 in Linux Fedora 23 Please tell my why Python3 doesn't work? Sincerely! Alexander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Enumerate vs DictReader object manipulation:
On 4 February 2016 at 03:21, Ben Finney wrote: > Alex Kleider writes: > >> How does a dict fit into this scheme? >> Is it a sequence? > > No, a dict is not a sequence. But it is a container: all its items > remain available and can be retrieved again and again, and you can > interrogate whether a value is one of the items in that container. > > An instance of the built-in ‘set’ type is also a container and not a > sequence. > > Containers are iterable too. You can see an explanation of the different collection terminology here: https://docs.python.org/2/library/collections.html#collections-abstract-base-classes A dict is a Mapping and a set is a Set. Both also comes under the categories Sized, Iterable, and Container. -- Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Enumerate vs DictReader object manipulation:
On 4 February 2016 at 06:45, Matt Williams wrote: > > Just as a note - you are not the only person caught out by this - it is a > very common slip. > > I wonder whether it would be worth adding a more explicit line about this > in the Python Docs? Where in the docs would you put it and what would you want it to say? -- Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Enumerate vs DictReader object manipulation:
Just as a note - you are not the only person caught out by this - it is a very common slip. I wonder whether it would be worth adding a more explicit line about this in the Python Docs? Matt On Wed, 3 Feb 2016 16:13 Ek Esawi wrote: > Hi All > > > > > > I have a code that reads a csv file via DictReader. I ran into a peculiar > problem. The python interpreter ignores the 2nd code. That is if I put the > reader iterator 1st, like the code below, the enumerate code is ignored; if > I put the enumerate code 1st, the reader code is ignored. I am curious to > know the nature of such behavior. EKE > > > > Here part of my code: > > > > . > > . > > . > > reader = csv.DictReader(MyFile) > > for row in reader: > > list_values = list(row.values()) > > print (list_values) > > > > for i,j in enumerate(reader): > > print(j) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor