-1

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')
2
  • 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, 2013 at 10:26
  • i got it... i declared pos but never printed it Dec 9, 2013 at 22:50

1 Answer 1

2

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.

Not the answer you're looking for? Browse other questions tagged .