Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am learning Python Programming from Full Circle Magazine. Can anybody correct this program as it is not working.

#!/usr/bin/env python
strng = 'The time has come'
pos = strng.find('time')
share|improve this question
1  
Define "not working" by mentioning which Python version you're using, how you run the script and what error output you get. –  Timo Nov 2 '13 at 10:26
    
i got it... i declared pos but never printed it –  mnstalemate Dec 9 '13 at 22:50
add comment

closed as unclear what you're asking by don.joey, Braiam, LiveWireBT, qbi, guntbert Nov 3 '13 at 20:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

I guess you are using Ubuntu and are trying to learn python. Here is a step by step guide on how to get your example working.

  1. Type terminal in the hud (the ubuntu symbol at the top left side of your screen) and hit enter. This opens a terminal.

  2. In the terminal type python. This opens an interactive programming shell for python.

  3. Then type your code. You will get your output immediately. I think you are struggling with your code because you do not know what to do with the pos variable. In the following example I just type pos to get the value of pos. You could also type print pos.

    strng = 'The time has come'
    pos = strng.find('time')
    pos 
    

The sort of output you are supposed to see is

>>> strng = 'the time has come'
>>> pos = strng.find('time')
>>> pos
4
>>> strng[4]
't'
>>> strng[4:]
'time has come'

If you want to be able to run this as a file, you need to proceed as follows.

  1. In your terminal type which python. This gives you the place of the binary executable file that is python.

  2. Put this "shebang" (#!/usr/bin/python) at the top of a file you save with the name you want.

  3. Run the command chmod +x the_file_name_you_choose.py in order to make your file executable.

  4. Then run in the same directory as your file python the_file_name_you_choose.py. If you only assign a few variables and do not call them (that is you say pos = ... but you never call pos) this will not give you any output though.

Let us know if there's anything else. If your problem is related to python rather than to not understanding how to use python in ubuntu, ask your question on stackoverflow.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.