[Tutor] The magic parentheses
Alan Gauld
alan.gauld at btinternet.com
Sun Jan 24 08:53:59 CET 2010
"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
by putting parens around the contents.To print them as a string use the
join() 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
and 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 to 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 sitehttp://www.alan-g.me.uk/
More information about the Tutor
mailing list