[Tutor] Tutor Digest, Vol 71, Issue 62
kwaku.nuamah at gmail.com
Mon Jan 25 07:25:14 CET 2010
On 1/24/10, tutor-request at python.org <tutor-request at python.org> wrote:
> Send Tutor mailing list submissions to
> tutor at python.org
> To subscribe or unsubscribe via the World Wide Web, visit
> or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
> You can reach the person managing the list at
> tutor-owner at python.org
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
> Today's Topics:
> 1. Re: The magic parentheses (Shashwat Anand)
> Message: 1
> Date: Sun, 24 Jan 2010 13:54:37 +0530
> From: Shashwat Anand <anand.shashwat at gmail.com>
> To: Alan Gauld <alan.gauld at btinternet.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] The magic parentheses
> <d4ab53de1001240024j3f1e48d6o1507f4e247ca24 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> Alan and rest have clarified your doubt I guess. However let me point you
> towards some other mistakes.
> def area1(radius):
> area1r = 3.14159 * mainvar1**2
> return area1r
> print area1r
> def area2(radius):
> area2r = 3.14159 * mainvar2**2
> return area2r
> print area2r
> Here you created two functions for the single purpose of calculating area.
> Why not just do,
> def area(radius):
> return math.pi * radius * radius
> Functions are created for doing repetitive tasks IMHO. But you just created
> them for the heck of it. Ofcourse there is issue of writing dead-code (i.e.
> lines that will never be executed), the print after return as Alan already
> pointed out.
> Also you created, a function that displays the difference between the two
> def return_difference_of12(var1,var2):
> if var1 - var2 > 0:
> y = v1,var1,v3,v2,var2,period
> print y
> elif var2 - var1 > 0:
> z = v2,var2,v3,v2,var1,period
> print z
> Why not create something simple like this :
> def return_difference(var1, var2):
> if var1 > var2:
> print 'Variable 1', var1, 'is greater than Variable 2', var2, '.'
> elif var2 > var1:
> print 'Variable 2', var2, 'is greater than Variable 1', var1, '.'
> print 'Variable 1', var1, 'is equal Variable 2', var2, '.'
> No need for explicit formation of tuple if all you wish is just a print
> On Sun, Jan 24, 2010 at 1:23 PM, Alan Gauld
> <alan.gauld at btinternet.com>wrote:
>> "David Hutto" <dwightdhutto at yahoo.com> wrote
>> This is my first post to the list, so tell me if I'm posting incorrectly.
>> You are doing fine. Welcome.
>> My problem is when the results are printed, I get this:
>>> ('Variable 2,', 490.0, 'is greater than', 'Variable 2,', 8.0, '.')
>> The parentheses, as well as the apostrophes and commas.
>>> I'm sure it's the way I'm having the results printed after it's
>>> through, but not sure how to correct it.
>> You have two problems in your code(at least!)
>> ---------------def area2(radius):
>> area2r = 3.14159 * mainvar2**2
>> return area2r
>> print area2r
>> --------The print statement will never be called because the return
>> statement forces an exit from the function.
>> def return_difference_of12(var1,var2):
>> if var1 - var2 > 0:
>> y = v1,var1,v3,v2,var2,period
>> print y
>> elif var2 - var1 > 0:
>> z = v2,var2,v3,v2,var1,period
>> print z
>> ----------------------------The assignments to y and z create tuples
>> (a,b,c...)So you are asking to print a tuple and Python represents tuples
>> putting parens around the contents.To print them as a string use the
>> method of a string using an empty string:print ''.join([str(s) for s in
>> y])However this is an unusual way to print this type of output,It would be
>> more normal to use a format string:print "Variable2: %f id greater than
>> Variable1: %f." % (var1,var2)This reduces the number of variables needed
>> also gives you much more control over layout because the %f markers can be
>> augmented with width specifiers, justificationhints etc.You can store the
>> entire format string in a variable if you wish - especiaslly if you want
>> use it multiple times - but in your case the strings only appear once so I
>> wouldn't bother.HTH,-- Alan GauldAuthor of the Learn to Program web
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
> -------------- next part --------------
> An HTML attachment was scrubbed...
> Tutor maillist - Tutor at python.org
> End of Tutor Digest, Vol 71, Issue 62
Sent from my mobile device
More information about the Tutor