[Tutor] textbook question

michael scott jigenbakuda at yahoo.com
Mon Feb 28 07:34:38 CET 2011


Hi, Justin, how are you?

Most of the nice people here like to see that you try before you ask for help. 
So next time post some code that you have tried (and failed on) so they can see 
WHERE you are going wrong or WHAT is throwing you off. I'm a beginner too, also 
I am using How to Think Like a Computer Scientist. So I really am similar to 
you.

But to give you a bit of help on your problem. Strings can actually have some 
basic math done on them (like addition and multiplication).

 for example

a = "Resident"
b = " Evil"

# you can perform addition on these two strings to join one big string.

#so... 
a + b = "Resident Evil"

#Like wise, multiplication can be done on a string (string times a number) to 
achieve a similar effect.

c = "Ha "
 c * 3 = "Ha Ha Ha"

So using my example can you think of a way to set up your function to augment 
strings to produce the effect that you desire? Give it a shot and if you get 
stuck post here again.

 ----
What is it about you... that intrigues me so?




________________________________
From: Justin Bonnell <jwbonnell5 at gmail.com>
To: tutor at python.org
Sent: Mon, February 28, 2011 12:36:43 AM
Subject: [Tutor] textbook question

In trying to learn Python, I'm reading through How to Think Like a Computer 
Scientist. I'm just on the third chapter but I'm getting stuck on this question:

Fill in the body of the function definition for cat_n_times so that it will 
print the string, s, n times:
def cat_n_times(s, n):
    <fill in your code here>
Save this function in a script named import_test.py. Now at a unix prompt, make 
sure you are in the same directory where the import_test.py is located ( ls 
should show import_test.py). Start a Python shell and try the following:
>>> from import_test import * >>> cat_n_times('Spam', 7) 
>>>SpamSpamSpamSpamSpamSpamSpam 
>>>
If all is well, your session should work the same as this one.  Experiment with 
other calls to cat_n_times until you feel comfortable with how it works.
I'm thinking it should be pretty easy but, again, I'm a beginner.
I copied the whole question but basically I just need some help with what the 
script should look like, that'd be great.


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110227/17052813/attachment.html>


More information about the Tutor mailing list