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')
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')
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.
Type terminal
in the hud (the ubuntu symbol at the top left side of your screen) and hit enter. This opens a terminal.
In the terminal type python
. This opens an interactive programming shell for python.
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.
In your terminal type which python
. This gives you the place of the binary executable file that is python.
Put this "shebang" (#!/usr/bin/python
) at the top of a file you save with the name you want.
Run the command chmod +x the_file_name_you_choose.py
in order to make your file executable.
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.