[Tutor] Working with strings
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 12 Mar 2001 16:52:07 -0000
------_=_NextPart_001_01C0AB14.C600A300
Content-type: text/plain; charset="iso-8859-1"
it at the moment). Can you correct my program below and give me some sample
program as to how to do it simply. I'm not having trouble with the logic of
the string manipulation(I think) - just getting the program to accept input
and print output.
Most replies have tried to give you a different function, I'll try to
work with this one...
First I'll indent it they way I think you meant it to be and use a non
proportional font for clarity:
def backword(bruce):
bruce=raw_input('input a word to reverse ')
index=len(bruce)-1
while index<0:
letter=bruce[index]
print letter,
index = index+1
>>> backword
<function backword at 00BC015C>
So we've defined a function which takes an argument, now to see if it
works...
>>> backword(flowerpot)
Traceback (innermost last):
File "<pyshell#36>", line 1, in ?
backword(flowerpot)
NameError: There is no variable named 'flowerpot'
We passed a variable parameter(not a string because there were no quote
signs
around it) and Python objected because the variable doesn't exist yet..
Fix it by declaring the variable before calling the function:
>>> flowerpot = "Flowerpot"
>>> backward(flowerpot)
Or by calling the function with a string:
>>> backward("flowerpot") # note the quotes...
>>> backword('flowerpot')
input a word to reverse flowerpot
OK, we entered the function. Now its asking us to replace the argument we
passed('flowerpot') with a new value we type at the prompt.
We now try to execute:
index=len(bruce)-1
find index of last letter
while index<0:
never do the loop stuff because the index of the last letter is greater than
0
exit the function.
>>> print bruce
harold # a value I entered earlier that it hasn't cleared.
correct!
>>> def backword('bruce'):
SyntaxError: invalid syntax
Try to define a new function but specify an argumemnt name of "bruce"
which is a string literal - Python doesn't allow string literals as
argument/variable
names so its a syntax error.
>>> backword(flowerpot)
Traceback (innermost last):
File "<pyshell#40>", line 1, in ?
backword(flowerpot)
NameError: There is no variable named 'flowerpot'
Back to the start of the message :-)
HTH,
Alan G.
------_=_NextPart_001_01C0AB14.C600A300
Content-type: text/html; charset="iso-8859-1"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 5.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<BLOCKQUOTE
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV><FONT face=Arial size=2>it at the moment). Can you correct my program
below and give me some sample program as to how to do it simply. I'm not
having trouble with the logic of the string manipulation(I think) - just
getting the program to accept input and print output.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV></BLOCKQUOTE>
<DIV><SPAN class=360213516-12032001><FONT color=#0000ff face=Arial size=2>Most
replies have tried to give you a different function, I'll try to
</FONT></SPAN></DIV>
<DIV><SPAN class=360213516-12032001><FONT color=#0000ff face=Arial size=2>work
with this one...</FONT></SPAN> <FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT
color=#0000ff>First I'll indent it they way I think you meant it to be and
use a non </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT
color=#0000ff>proportional font for clarity:</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001> </SPAN><BR></FONT><FONT
face=Arial></FONT></FONT><FONT face="Courier New" size=2>def
backword(bruce):<BR><SPAN class=360213516-12032001><FONT color=#0000ff><FONT
color=#000000> </FONT> </FONT></SPAN> bruce=raw_input('input
a word to reverse ')<BR><SPAN class=360213516-12032001><FONT
color=#0000ff> <FONT
color=#000000> </FONT> </FONT></SPAN> index=len(bruce)-1<BR> <SPAN
class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN>while
index<0:<BR> <SPAN class=360213516-12032001><FONT
color=#0000ff>
</FONT></SPAN>letter=bruce[index]<BR> <SPAN
class=360213516-12032001><FONT color=#0000ff>
</FONT></SPAN>print letter,<BR> <SPAN
class=360213516-12032001><FONT color=#0000ff>
</FONT></SPAN>index = index+1</FONT><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001> </SPAN> <BR></FONT><FONT
face="Courier New">>>> backword<BR><function backword at
00BC015C><BR><SPAN class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001>So we've defined a function which takes an argument,
now to see if it works...</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT face="Courier New"><FONT size=2><SPAN
class=360213516-12032001> </SPAN>>>>
backword(flowerpot)<BR>Traceback (innermost last):<BR> File
"<pyshell#36>", line 1, in ?<BR>
backword(flowerpot)<BR>NameError: There is no variable named 'flowerpot'<SPAN
class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001>We passed a variable parameter(not a string because
there were no quote signs </SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001>around it) and Python objected because the variable
doesn't exist yet.. </SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001>Fix it by declaring the variable before calling the
function:</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT color=#0000ff face="Courier New"><SPAN
class=360213516-12032001>>>> flowerpot =
"Flowerpot"</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face="Courier New"><SPAN
class=360213516-12032001>>>>
backward(flowerpot)</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001>Or by calling the function with a
string:</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT color=#0000ff face="Courier New"><SPAN
class=360213516-12032001>>>> backward("flowerpot") # note the
quotes...</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001> </SPAN><BR></FONT><FONT
face="Courier New">>>> backword('flowerpot')<BR>input a word to
reverse flowerpot<SPAN class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT
color=#0000ff>OK, we entered the function. Now its asking us to replace the
argument we </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT
color=#0000ff>passed('flowerpot') with a new value we type at the
prompt.</FONT></SPAN></FONT></FONT></DIV>
<DIV><SPAN class=360213516-12032001></SPAN><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001><FONT color=#0000ff> We now try to
execute:</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT face="Courier New"><FONT size=2><SPAN class=360213516-12032001><FONT
color=#0000ff><SPAN class=360213516-12032001><FONT color=#0000ff> <FONT
color=#000000> </FONT> </FONT></SPAN> index=len(bruce)-1</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><SPAN class=360213516-12032001><FONT color=#0000ff
face=Arial>find index of last letter</FONT></SPAN></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT
color=#0000ff><BR><FONT face="Courier New"> <SPAN
class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN>while index<0:<BR></FONT><FONT
face=Arial>never do the loop stuff because the index of the last letter is
greater than 0</FONT></FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN
class=360213516-12032001>exit the function.</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT face=Arial size=2><FONT face="Courier New">>>> print
bruce<BR></FONT>harold # a value I entered earlier that it hasn't cleared.<SPAN
class=360213516-12032001><FONT color=#0000ff> </FONT></SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=360213516-12032001>correct!</SPAN></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><SPAN
class=360213516-12032001> </SPAN><BR><FONT face="Courier New"></FONT><FONT
size=2>>>> def backword('bruce'):<BR></FONT></FONT><FONT
size=2> SyntaxError: invalid syntax<SPAN class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN
class=360213516-12032001>Try to define a new function but specify an argumemnt
name of "bruce" </SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN
class=360213516-12032001>which is a string literal - Python doesn't allow string
literals as argument/variable </SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN
class=360213516-12032001>names so its a syntax error.</SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN
class=360213516-12032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT face=Arial size=2>>>> backword(flowerpot)<BR>Traceback
(innermost last):<BR> File "<pyshell#40>", line 1, in
?<BR> backword(flowerpot)<BR>NameError: There is no variable
named 'flowerpot'<BR><SPAN class=360213516-12032001><FONT
color=#0000ff> </FONT></SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=360213516-12032001>Back
to the start of the message :-)</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=360213516-12032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=360213516-12032001>HTH,</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=360213516-12032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=360213516-12032001>Alan
G.</SPAN></FONT></DIV></BODY></HTML>
------_=_NextPart_001_01C0AB14.C600A300--