[Tutor] inputting strings into functions

alan.gauld@bt.com alan.gauld@bt.com
Thu, 15 Mar 2001 22:29:45 -0000


------_=_NextPart_001_01C0AD9F.6FD6D750
Content-type: text/plain; charset="iso-8859-1"


I have tried various incarnations of an algorithm as below:
 
def countletters("string","letter"):
 

This tries to create a funtion with literal strings as parameters.
Parameters 
are a special type of variable and obey the same naming rules thus you can't
use 
quotes around them...

SyntaxError: invalid syntax   (the closing quote(") on the first parameter
of the function definition is highlighted.

So Python correctly complains that the variable is badly named. 

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 = 'string'
 
Oh, so close! you want to make the variable fruit hold the same as the 
parameter/variable string so you don't want the quotes:
 
fruit = string
 
What you did was to make fruit hold the literal string 'string'.
 

  if char == 'letter': 
 

Same here, you want to compare char to the value of the parameter 
not to the literal string 'letter' :
 
if char == letter
 

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

This is because you  are now passing a name(no quotes) into the function.
You need to pass a literal string:
>>> countletters("kingmaker","k")
 
You need to go back to your textbook and read up on the difference between 
variable names and values. In particular play with assigning strings to
variables 
and printing the results. Comparing them to other strings etc. eg:
 
>>> var = "variable"
>>> var2 = "another"
>>> print var
>>> print "variable"
>>> print variable
>>> print var2
>>> if var2 == "another" : print "another"
>>> if var2 == another : print "another"
>>> if var2 == "another"v : print another
 
etc...
 
Try these and look carefully at what hapens and keep at it till you
understand 
exactly whats happening.
 


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 biased but you might find it helpful to compare HtTLaCS with my tutorial
when 
you get stuck - its not necessarily better but gives a different perspective
:-)
 
http://www.crosswinds.net/~agauld/ <http://www.crosswinds.net/~agauld/> 
 
For this case look at the following sections:
simple sequences,
raw materials(first few sections only),
more sequences,
modules and functions
 
 
HTH,
 
Alan G

------_=_NextPart_001_01C0AD9F.6FD6D750
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"><SPAN 
  class=code>
  <DIV>I have tried various incarnations of an algorithm as below:</DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT size=2><FONT face=Arial>def 
  countletters("string","letter"):<BR><SPAN class=050551122-15032001><FONT 
  color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV></BLOCKQUOTE>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001>This tries to create a funtion with literal strings as 
parameters. Parameters </SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001>are a special type of variable and obey the same naming 
rules thus you can't use </SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001>quotes around them...</SPAN></FONT></FONT></DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT size=2><FONT face=Arial>SyntaxError: invalid syntax<EM>&nbsp;&nbsp; 
  (the closing quote(") on the first parameter of the function definition is 
  highlighted.</EM></FONT></FONT></DIV></BLOCKQUOTE>
<DIV><SPAN class=050551122-15032001><FONT color=#0000ff face=Arial size=2>So 
Python correctly complains that the variable is badly 
named.</FONT></SPAN>&nbsp;</DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT face=Arial size=2>I have tried it without the quotes but it still 
  doesn't work as it gives me the message underneath this code:</FONT></DIV>
  <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BLOCKQUOTE>
<DIV><FONT size=2><FONT face=Arial>def 
countletters(string,letter):<BR>&nbsp;<SPAN class=050551122-15032001><FONT 
color=#0000ff>&nbsp; &nbsp;</FONT></SPAN>fruit = 'string'<BR><SPAN 
class=050551122-15032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001>Oh, so close! you want to make the variable fruit hold 
the same as the </SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001>parameter/variable string so you don't want the 
quotes:</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001>fruit = string</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001>What you did was to make fruit hold the literal string 
'string'.</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=050551122-15032001></SPAN></FONT></FONT>&nbsp;</DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT face=Arial><FONT size=2>&nbsp;&nbsp;if char == 'letter':<FONT 
  color=#0000ff></FONT><FONT size=2><SPAN 
  class=050551122-15032001>&nbsp;</SPAN></FONT></FONT></FONT></DIV>
  <DIV><FONT face=Arial><FONT color=#0000ff><FONT size=2><SPAN 
  class=050551122-15032001></SPAN></FONT></FONT></FONT>&nbsp;</DIV></BLOCKQUOTE>
<DIV><FONT face=Arial><FONT color=#0000ff><FONT size=2><SPAN 
class=050551122-15032001><FONT color=#000000>Same here, you want to compare char 
to the value of the parameter </FONT></SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff><FONT size=2><SPAN 
class=050551122-15032001><FONT color=#000000>not to the literal string 
'letter'</FONT>&nbsp;<FONT 
color=#000000>:</FONT></SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff><FONT size=2><SPAN 
class=050551122-15032001></SPAN></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001></SPAN><FONT 
size=2><FONT color=#000000><FONT face=Arial><SPAN class=050551122-15032001>if 
char == letter</SPAN></FONT></FONT></FONT></FONT></DIV>
<DIV><FONT color=#0000ff><FONT size=2><FONT color=#000000><FONT face=Arial><SPAN 
class=050551122-15032001></SPAN></FONT></FONT></FONT></FONT>&nbsp;</DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT face=Arial size=2>&gt;&gt;&gt; 
  countletters(kingmaker,k)<BR>Traceback (innermost last):<BR>&nbsp; File 
  "&lt;pyshell#16&gt;", line 1, in ?<BR>&nbsp;&nbsp;&nbsp; 
  countletters(kingmaker,k)<BR>NameError: There is no variable named 
  'kingmaker'</FONT></DIV></BLOCKQUOTE>
<DIV><SPAN class=050551122-15032001><FONT color=#0000ff face=Arial size=2>This 
is because you&nbsp; are now passing a name(no quotes) into the 
function.</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>You need to pass a literal 
string:</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; 
countletters("kingmaker","k")</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN>&nbsp;</DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>You need to go back to your textbook and read up 
on the difference between </FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001><FONT color=#0000ff face=Arial 
size=2>variable names and values. In particular play with assigning strings to 
variables </FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001><FONT color=#0000ff face=Arial size=2>and 
printing the results. Comparing them to other strings etc. 
eg:</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN>&nbsp;</DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; var = 
"variable"</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; var2 = 
"another"</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; print var</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; print 
"variable"</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; print variable</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; print var2</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; if var2 == "another" : print 
"another"</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; if var2 == another : print 
"another"</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>&gt;&gt;&gt; if var2 == "another"v : print 
another</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN>&nbsp;</DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>etc...</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN>&nbsp;</DIV>
<DIV><SPAN class=050551122-15032001></SPAN><SPAN class=050551122-15032001><FONT 
color=#0000ff face=Arial size=2>Try these and look carefully at what hapens and 
keep at it till you understand </FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001><FONT color=#0000ff face=Arial 
size=2>exactly whats happening.</FONT></SPAN></DIV>
<DIV><SPAN class=050551122-15032001></SPAN>&nbsp;</DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px"><FONT 
  size=2><FONT face=Arial>
  <DIV>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&nbsp;<FONT color=#0000ff><SPAN 
  class=050551122-15032001>&nbsp;</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff><SPAN 
  class=050551122-15032001></SPAN></FONT>&nbsp;</DIV></BLOCKQUOTE>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>I'm biased but you might 
find it helpful to compare HtTLaCS with my tutorial when </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>you get stuck - its not 
necessarily better but gives a different perspective :-)</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN 
class=050551122-15032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001><A 
href="http://www.crosswinds.net/~agauld/">http://www.crosswinds.net/~agauld/</A></SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN 
class=050551122-15032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>For this case look at 
the following sections:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>simple 
sequences,</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>raw materials(first few 
sections only),</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>more 
sequences,</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>modules and 
functions</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN 
class=050551122-15032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff><SPAN 
class=050551122-15032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>HTH,</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff><SPAN 
class=050551122-15032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff><SPAN class=050551122-15032001>Alan 
G</SPAN></FONT></SPAN></FONT></FONT></DIV></BODY></HTML>

------_=_NextPart_001_01C0AD9F.6FD6D750--