[Tutor] inputting strings into functions

Paul Jones paulnsharon@dingoblue.net.au
Thu, 15 Mar 2001 15:48:35 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_0015_01C0AD67.651CD900
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi-back again- I'm trying with little success to figure out how to input =
strings as parameters into a function definition. The problem is as =
follows.
I have been given the following algorithm {in brackets}=20
{The following program counts the number of times the letter 'a' appears =
in a string:

  fruit =3D "banana"=20
  count =3D 0=20
  index =3D 0=20
  for char in fruit:=20
    if char =3D=3D 'a':=20
      count =3D count + 1=20
  print count           }

and asked to encapsulate this code in a function named countLetters, and =
generalize it so that it accepts the string and the letter as arguments. =


I have tried various incarnations of an algorithm as below:

def countletters("string","letter"):
 fruit =3D 'string'
 count=3D0
 index=3D0
 for char in fruit:
  if char =3D=3D 'letter':
   count=3Dcount+1
 print count
=20
SyntaxError: invalid syntax   (the closing quote(") on the first =
parameter of the function definition is highlighted.

I have tried it without the quotes but it still doesn't work as it gives =
me the message underneath this code:

def countletters(string,letter):
 fruit =3D 'string'
 count=3D0
 index=3D0
 for char in fruit:
  if char =3D=3D 'letter':
   count=3Dcount+1
 print count

=20
>>> countletters(kingmaker,k)
Traceback (innermost last):
  File "<pyshell#16>", line 1, in ?
    countletters(kingmaker,k)
NameError: There is no variable named 'kingmaker'

How can I write this so it will work and the end user doesn't have to =
input anything except the parameters of the function.
The tutorial I'm doing(How to Think Like a Computer Scientist) has given =
examples of integer parameters but not how to do this with strings. I'm =
sure there is an easy way to do this but I'm obviously not finding it.
Regards.
Paul Jones
email: paulnsharon@dingoblue.net.au

------=_NextPart_000_0015_01C0AD67.651CD900
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4134.600" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi-back again- I'm trying with little =
success to=20
figure out how to input strings as parameters into a function =
definition. The=20
problem is as follows.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I have been given the following =
algorithm&nbsp;{in=20
brackets}&nbsp;</FONT></DIV>
<P>{The following program counts the number of times the letter <SPAN=20
class=3Dcode><SPAN class=3Dquote><FONT face=3DCourier=20
color=3D#ff0000>'a'</FONT></SPAN></SPAN> appears in a string:</P>
<DIV><SPAN class=3Dcode><FONT face=3DCourier>&nbsp; fruit =3D <SPAN =
class=3Dquote><FONT=20
color=3D#ff0000>"banana"</FONT></SPAN> <BR>&nbsp; count =3D 0 <BR>&nbsp; =
index =3D 0=20
<BR>&nbsp; <SPAN class=3Dkeyword><FONT color=3D#0000ff>for</FONT></SPAN> =
char <SPAN=20
class=3Dkeyword><FONT color=3D#0000ff>in</FONT></SPAN> fruit: <BR>&nbsp; =
&nbsp;=20
<SPAN class=3Dkeyword><FONT color=3D#0000ff>if</FONT></SPAN> char =3D=3D =
<SPAN=20
class=3Dquote><FONT color=3D#ff0000>'a'</FONT></SPAN>: <BR>&nbsp; &nbsp; =
&nbsp;=20
count =3D count + 1 <BR>&nbsp; <SPAN class=3Dkeyword><FONT=20
color=3D#0000ff>print</FONT></SPAN>=20
count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}</FONT></SPAN></DIV><SPAN class=3Dcode><FONT face=3DCourier></FONT>
<DIV><BR><FONT face=3DArial size=3D2>and asked to <FONT =
size=3D3><EM><FONT=20
face=3D"Times New Roman">encapsulate this code in a function named =
</FONT><SPAN=20
class=3Dcode><FONT face=3DCourier>countLetters</FONT></SPAN><FONT=20
face=3D"Times New Roman">, and generalize it so that it accepts the =
string and the=20
letter as arguments.</FONT></EM><FONT face=3D"Times New Roman">=20
</FONT></FONT></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>I have tried various incarnations of an algorithm as below:</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>def =
countletters("string","letter"):<BR>&nbsp;fruit=20
=3D 'string'<BR>&nbsp;count=3D0<BR>&nbsp;index=3D0<BR>&nbsp;for char in=20
fruit:<BR>&nbsp;&nbsp;if char =3D=3D=20
'letter':<BR>&nbsp;&nbsp;&nbsp;count=3Dcount+1<BR>&nbsp;print=20
count<BR>&nbsp;<BR>SyntaxError: invalid syntax<EM>&nbsp;&nbsp; (the =
closing=20
quote(") on the first parameter of the function definition is=20
highlighted.</EM></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I have tried it without the quotes but =
it still=20
doesn't work as it gives me the message underneath this =
code:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>def =
countletters(string,letter):<BR>&nbsp;fruit =3D=20
'string'<BR>&nbsp;count=3D0<BR>&nbsp;index=3D0<BR>&nbsp;for char in=20
fruit:<BR>&nbsp;&nbsp;if char =3D=3D=20
'letter':<BR>&nbsp;&nbsp;&nbsp;count=3Dcount+1<BR>&nbsp;print =
count</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;<BR>&gt;&gt;&gt;=20
countletters(kingmaker,k)<BR>Traceback (innermost last):<BR>&nbsp; File=20
"&lt;pyshell#16&gt;", line 1, in ?<BR>&nbsp;&nbsp;&nbsp;=20
countletters(kingmaker,k)<BR>NameError: There is no variable named=20
'kingmaker'</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>How can I write this so it will work =
and the end=20
user doesn't have to input anything except the parameters of the=20
function.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>The tutorial I'm doing(How to Think =
Like a Computer=20
Scientist) has given examples of integer parameters but not how to do =
this with=20
strings. I'm sure there is an easy way to do this but I'm obviously not =
finding=20
it.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Regards.</FONT></SPAN></DIV>
<DIV><FONT face=3DArial size=3D2>Paul Jones<BR>email: <A=20
href=3D"mailto:paulnsharon@dingoblue.net.au">paulnsharon@dingoblue.net.au=
</A></FONT></DIV></BODY></HTML>

------=_NextPart_000_0015_01C0AD67.651CD900--