[Tutor] The magic parentheses
Hi, This is my first post to the list, so tell me if I'm posting incorrectly. I'm creating a script, http://python.codepad.org/mHyqbJ2z that gives the area of two circles, based on their radius, and displays the difference between the two results. My problem is when the results are printed, I get this: Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32 IDLE 2.6.4 No Subprocess >>> ('Variable 2,', 490.0, 'is greater than', 'Variable 2,', 8.0, '.') >>> ('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. I tried writing the 'Variable 1' and '2', as well as the 'is greater than' within the y, and z local variables in the def return_difference_of12, and got the same result as when I listed the portions of the printed result's sentence in the non-local variables I have now(I'm new to Python, so I'm not sure if this would be the correct term). Any help would be appreciated. Thanks in advance. David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The magic parentheses
On 01/24/10 17:14, David Hutto wrote: > Hi, > > This is my first post to the list, so tell me if I'm posting incorrectly. > > I'm creating a script, http://python.codepad.org/mHyqbJ2z that gives the area > of two circles, based on their radius, and displays the difference between > the two results. > > My problem is when the results are printed, I get this: > > Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] > on win32 > IDLE 2.6.4 No Subprocess > ('Variable 2,', 490.0, 'is greater than', 'Variable 2,', 8.0, '.') > ('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. > > I tried writing the 'Variable 1' and '2', as well as the 'is greater > than' within the y, and z local variables in the def return_difference_of12, > and got the same result as when I > listed the portions of the printed result's sentence in the non-local > variables I have now(I'm new to Python, so I'm not sure if this would be the > correct term). > > Any help would be appreciated. Thanks in advance. > OT: for short snippets like this, you should just paste it in the post rather than using codepad """ y = v1,var1,v3,v2,var2,period print y """ is not the same as """ print v1,var1,v3,v2,var2,period """ the first code creates a tuple, assign it to y, then prints the tuple; the latter prints the bunch of items separated with space. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The magic parentheses
On Sun, Jan 24, 2010 at 7:14 AM, David Hutto wrote: > Hi, > > This is my first post to the list, so tell me if I'm posting incorrectly. > > I'm creating a script, http://python.codepad.org/mHyqbJ2z that gives the > area of two circles, based on their radius, and displays the difference > between the two results. > > My problem is when the results are printed, I get this: > > Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit > (Intel)] on win32 > IDLE 2.6.4 No Subprocess > >>> > ('Variable 2,', 490.0, 'is greater than', 'Variable 2,', 8.0, '.') > >>> > ('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. > > I tried writing the 'Variable 1' and '2', as well as the 'is greater than' > within the y, and z local variables in the def return_difference_of12, and > got the same result as when I listed the portions of the printed result's > sentence in the non-local variables I have now(I'm new to Python, so I'm not > sure if this would be the correct term). > > Any help would be appreciated. Thanks in advance. > > > David > > y = v1,var1,v3,v2,var2,period print y The first line assigns a tuple to y. The parentheses and commas is the way tuples are printed. To avoid those, you could either pass all the arguments directly to print, or use some string formatting instead of constructing a tuple: >>> a = 'this', 'is', 'a tuple' >>> a ('this', 'is' , 'a tuple') >>> print a (1, 2, 3) >>> print 'this', 'is passed', 'to print directly' 1 2 3 >>> print "this is {0}".format("formatted") this is formatted Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The magic parentheses
"David Hutto" 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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor