LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-04-2009, 10:20 AM   #1
danred
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Rep: Reputation: 0
Python shell help (using elif)


I am using the shell interpreter for python2.6.2 on Ubuntu Jaunty, when I try to use the elif statement I get invalid syntax error.

I don't think it has to do with indentation or anything on my part. It seems like the shell isn't able to handle it because I can copy and paste my code in the edit window then save and execute and it will run as expected.

When I type elif it changes color but when I hit enter to go to the next line instead of automatically indenting it gives me the invalid syntax error. I have typed it fifty different ways so I don't think it has anything to with syntax or indentation as it will work if I save it as a program and then execute it.

I am doing a tutorial for python and it would be helpful to run everything from the python shell interpreter so I don't have to keep saving everything

Last edited by danred; 10-04-2009 at 10:21 AM.
 
Old 10-04-2009, 12:40 PM   #2
gtglus
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian GNU/Linux
Posts: 5

Rep: Reputation: 0
Hi Danred,


You may consider the warning posted in the Python official site:

http://www.python.org/download/releases/2.6.2/

and try to install the Python2.6.3 ...

If it fails again, post in this forum what you try to do.

Cheers,

Glus
 
Old 10-09-2009, 10:50 PM   #3
danred
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
I tried upgrading but that didn't work. I was hoping it was something like a missing library, because as far as documentation goes I am the only one who has mentioned it and I find it hard to believe that I am the only one who uses Ubuntu and the shell interpreter.
 
Old 10-15-2009, 09:58 AM   #4
gtglus
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian GNU/Linux
Posts: 5

Rep: Reputation: 0
Hi Danred,

It's very important to know more exactly the steps you do/done. For example, if you got a syntax error, a good idea could be to post the exact code (or a piece of it) you tried to run.

Otherwise, you could try to define this example code (be aware with indentations and other characters like ':'...)

Code:
>>> def level(a):
...     if a>0:
...         print "positive number"
...     elif a<0:
...         print "negative number"
...     else:
...         print "zero"
And tell us the problems you find.

To run (and check) this programm, you simply could do:

Code:
>>> level (1)
positive number
>>> level (0)
zero
>>> level (-1)
negative number
>>> level (-6)
negative number
>>> level (0.4)
positive number
Cheers,

Glus
 
Old 10-16-2009, 12:02 AM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by danred View Post
I am using the shell interpreter for python2.6.2 on Ubuntu Jaunty, when I try to use the elif statement I get invalid syntax error.

I don't think it has to do with indentation or anything on my part. It seems like the shell isn't able to handle it because I can copy and paste my code in the edit window then save and execute and it will run as expected.

When I type elif it changes color but when I hit enter to go to the next line instead of automatically indenting it gives me the invalid syntax error. I have typed it fifty different ways so I don't think it has anything to with syntax or indentation as it will work if I save it as a program and then execute it.

I am doing a tutorial for python and it would be helpful to run everything from the python shell interpreter so I don't have to keep saving everything
How do you know python is being called to interpret the script? This isn't automatic, you know -- you have to take certain steps to assure that your script gets interpreted by Python. Based on your report, the code works in one environment but not another -- this makes me think the system is invoking the wrong interpreter.

And, as usual, no code, no clue about the nature of the script.
 
Old 10-17-2009, 10:36 AM   #6
danred
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks you guys for taking the time to try and help me solve this problem, I havn't been able to get online in a while but any help is appreciated.

The code that glus posted is almost identical to the example I have been trying to execute

Quote:
>>> x=5
>>> if x<5:
y=-1
z=5
elif x>5:
after hitting enter after the last line is when I recieve the syntax error and is does not matter whether or not if "if" and "elif" indentations are aligned or not
 
Old 10-17-2009, 10:38 AM   #7
danred
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
sorry It wont let me post code the way it looks in the interpreter but the lines have the proper indentations and the elif statement is indented less than y and z but is aligned with the if statement

Last edited by danred; 10-17-2009 at 10:43 AM. Reason: trying to post code example
 
Old 10-17-2009, 10:51 AM   #8
danred
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Code:
>>>x = 5
>>>if x < 5:
       y = -1
       z = 5
   elif x > 5:
       y = 1
       z = 11
   else:
       y = 0
       z = 10
   print x, y, z,
this is how the example appears in the book
and this is as far as it will let me enter B4 giving me a syntax error

Code:
>>>x = 5
>>>if x < 5:
       y = -1
       z = 5
   elif X > 5:
I have tried diffrent indentation schemes but still get error and like I said if I use edit window and save and execute it works fine

Last edited by danred; 10-17-2009 at 10:53 AM.
 
Old 10-20-2009, 03:55 AM   #9
gtglus
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian GNU/Linux
Posts: 5

Rep: Reputation: 0
Hi, Danred,

I didn't yet find your problem. Using the interactive mode, I test your code in,

a) Python 2.6.3 and 3.1.1 (Debian Squeeze environment -system installed)
b) Python 2.6.2 (Ubuntu Jaunty 9.04 -working directly inside a Live CD session)

And no problem occurs... further than... in interactive mode you could break the "print" statement from the previous code unless you know the way to join the "if...elif...else" structure with it (I don't).

Although, note that this runs,

Quote:
>>> x=5; print x
To continue checking you could try to do what I make in b)... and tell us.

Cheers,

Glus
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script if elif problem assyrian1 Linux - Software 2 12-09-2008 09:00 AM
python: can you update the current shell from python? BrianK Programming 4 01-31-2008 12:18 AM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM
elif??????????? embsupafly Programming 1 11-27-2002 03:48 AM
getting out of the Python Shell! Colonel Panic Programming 2 08-19-2001 06:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration