Python syntax

Can anyone tell me why 'except KeyboardInterrupt: is invalid?

Malcolm Smmith

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
Loading thread data ...

Are you 'try:'-ing? It works here.

try: print "press break"; while True: pass except KeyboardInterrupt: print "it broke!"

--
umop apisdn
Reply to
Jasen Betts

Jasen Typed your prog into Idle3 window and ran it in LXterm. window. It gave me invalid syntax with arrow pointing to the t of except.

Malcolm

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

I'll take a flying guess and assume that you're typing the text straight into the debugger and getting the indenting wrong. Don't do that unless you're testing commands longer than a line or two. (And if you must, it was probably just a case of not hitting backspace before the "except" to remove the indent.)

Start off with: file -> new file. Then cut-and-paste Jasen's example into the new window. Then save it. Then run-module. THEN look at the debugger window.

Reply to
Dave Farrance

What a faff. Two ssh shells one with an editor to adjust the code, the other to run it in. B-)

--
Cheers 
Dave.
Reply to
Dave Liquorice

How about you tell us if you are using python3 or python2 first?

Reply to
mm0fmf

I seem to have got Jasen's module working.As written I got some syntax errors but finished up with :-

#!/usr/bin/env python print ("Press break") while True: pass exceptKeyboardInterrupt; print("It broke");

This was typed in the 'edit' window of Idle3 and saved; then run using the command window of LXTerm. On running the shell window displayed Press break. the curser kept flashing until I did a Ctrl-C when the command window displayed "..... while True - Keyboard interupt" and control was restored. Prior attempts using Break key froze the system making a reboot neccesary. Looks untidy but did it work as expected.

Malcolm

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

Python 3.2

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

Yep. As for myself, after looking at various Python editors that come with Ubuntu, I settled on Spyder as having a reasonable compromise between a good selection of features and not being cluttered.

Reply to
Dave Farrance

Nope. That's still not right. If it says "KeyboardInterrupt" then the exception still isn't activated correctly. It should print the message in the last line when you press ctrl-c. I see that you've got no space after the "except" which would explain that fault.

Reply to
Dave Farrance

Note that you normally put "!/usr/bin/env python3" as the first line then, because most OSs still default to python 2.

I've tried updating myself to Python 3, but gave up after finding that even the IDEs that are supposed to work with Python 3 are troublesome, and some useful Python 3 libraries have yet to make their way into the Linux distros. I think I'll wait another year or two.

Reply to
Dave Farrance

With a space after except I get a syntax error with an arrow pointing at the 't' of accept??

Malcolm

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

"except" not "accept". Ho hum. Also you appear to have missed out the "try". Also check the indenting which should be _exactly_ as shown in Jasen's example. The "try" and "except" should _not_ be indented but the other lines should. And don't type it in if you're having that much finger trouble. Start off by cutting-and-pasting it. Get it to work. Then proceed with extra code after that.

Reply to
Dave Farrance

The accept was a typo I have been typing except but the 'try' I must check out.

I use the Iyonix for mail so cutting/pasting is not so strait forward.

Malcolm

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

Well if you are using python3.2 you should use Python3 statements not Python2 statements.

In Python3

print 'hello world'

becomes

print('hello world')

If you are following a book or tutorial it should have said that it was using Python2 or 3 at the start and if you have never programmed Python before, using the same version of Python as the guide would be a big help for you.

You can get Python2 for all the rPi Linux distros. If you are using something other than Linux on your rPi you may only have Python3.

Andy

Reply to
mm0fmf

"try:"

is part of the python source, not an instruction to the operator. cut and paste that whole block

It should run without edits and say "it broke" when you send ctrl-c

--
umop apisdn
Reply to
Jasen Betts

Not while he's typing Python2 source into a Python3 interpreter!

Reply to
mm0fmf

Raspbian includes both Python2 and Python3, I think. In any case, it defaults to Python2 - I know because that's how I've been using it :) .

I really wouldn't recommend that a beginner should start with anything other than Raspbian. There is an awful lot of support for that distro among the RPi community.

Reply to
mick

Yes I found out that try: was code after Google found a list of keywords.

Using idle3 I always get a syntax error using try: and except.

Hashing out the try: & expect lines the program runs without syntax errors.

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

Wise words. But I am learning. Yes, on page 134 I find 'we are going to use Python 2.x since that is the most likely that you will have installed. I have only reached page 98 so far.

Do I understand correctly that these codes will run OK if typed into Idle as opposed to Idle3?.

Malcolm

--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.