5 Replies - 660 Views - Last Post: 24 August 2012 - 02:06 PM Rate Topic: -----

#1 deftones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 12

using strings in a function

Posted 23 August 2012 - 01:26 PM

This is in the first assignment at uni comp sci class and I'm already having trouble =( writing the math functions was easy, but now I can't figure this out.
#use the color and rhyme inputs in the poem
def writePoem (color, rhyme):
    print ('I\ve never written code before, or thought about this',rhyme,'But I\d rather get my homework done, than swim in something',color);


writePoem(brown, town)
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
builtins.NameError: name 'brown' is not defined

Is This A Good Question/Topic? 0
  • +

Replies To: using strings in a function

#2 deftones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 12

Re: using strings in a function

Posted 23 August 2012 - 02:13 PM

Well I figured out I have to put writePoem("brown", "town"). was missing quotations.
Was This Post Helpful? 0
  • +
  • -

#3 deftones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 12

Re: using strings in a function

Posted 23 August 2012 - 02:58 PM

Is there a way to do this without needing the quotation marks so that writePoem(brown, town) will work?
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,686
  • Joined: 13-March 10

Re: using strings in a function

Posted 23 August 2012 - 05:26 PM

brown = "brown"
poem = "poem"

function(brown,poem)
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: using strings in a function

Posted 23 August 2012 - 05:27 PM

Well, yes. You will need quotes somewhere for the literal strings, but you can store them in named variables before you call the function i.e.:

#use the color and rhyme inputs in the poem
def writePoem (color, rhyme):
    print ('I\ve never written code before, or thought about this',rhyme,'But I\d rather get my homework done, than swim in something',color);
...
>>> brown = "brown"
>>> town = "town"
>>> writePoem(brown, town)
Ive never written code before, or thought about this town But Id rather get my homework done, than swim in something brown


Was This Post Helpful? 0
  • +
  • -

#6 den510  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-August 12

Re: using strings in a function

Posted 24 August 2012 - 02:06 PM

Personally, I prefer to use this method, simply because you see exactly what you're printing. I've never been a fan of commas, more of concatenation:


def writePoem (color, rhyme):
    print ('I\ve never written code before, or thought about this '+rhyme+', But I\d rather get my homework done, than swim in something '+color+'.')

brown, town = "brown", "town"

writePoem(brown, town)
exit()




Hope this helps, best of luck with your programming!

-Dennis
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1