Re: [Tutor] Help Passing Variables
David Hutto wrote: > #A little more complex in terms of params: > > def SwapCaseAndCenter(*kwargs): > > if upper_or_lower == "upper": > print a_string.center(center_num).upper() > > if upper_or_lower == "lower": > print a_string.center(center_num).lower() > > a_string = raw_input("Give me a word, or letter: ") > upper_or_lower = raw_input("upper, or lower character(s): ") > center_num = int(raw_input("Where should number be centered?: ")) > SwapCaseAndCenter(a_string, upper_or_lower, center_num) > > This function is using the global variables for each of its variables. To use the versions passed in you need to define the names from kwargs; the name "kwargs" is usually used in reference to a keyword arguments which should be denoted by double asterisk instead of a single one. In this case kwargs is confusingly a positional list of arguments (sometimes referred to as "args" instead). For a function called SwapCaseAndCenter, I see it doing no case "swapping" unless it refers to changing the entire string to a specific case? At the very least, no centering is being done. To format I use string formatting[0] which has its own syntax[1]. >>> '{0:^40}'.format( 'test' ) # The below line is the repr ' test ' >>> '{1:^{0}}'.format( 40, 'test' ) # Dynamically generate width ' test ' >>> '{0:^{1}}'.format( 'test', 40 ) ' test ' [0]: http://docs.python.org/2/library/string.html#formatstrings [1]: http://docs.python.org/2/library/string.html#formatspec Ramit Prasad This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
#Apologies, this is the actual code: def SwapCaseAndCenter(a_string, upper_or_lower = None, center_num = None): if upper_or_lower == "upper": print a_string.center(center_num).upper() if upper_or_lower == "lower": print a_string.center(center_num).lower() a_string = raw_input("Give me a word, or letter: ") upper_or_lower = raw_input("upper, or lower character(s): ") center_num = int(raw_input("Where should number be centered?: ")) SwapCaseAndCenter(a_string, upper_or_lower, center_num) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
#A little more complex in terms of params: def SwapCaseAndCenter(*kwargs): if upper_or_lower == "upper": print a_string.center(center_num).upper() if upper_or_lower == "lower": print a_string.center(center_num).lower() a_string = raw_input("Give me a word, or letter: ") upper_or_lower = raw_input("upper, or lower character(s): ") center_num = int(raw_input("Where should number be centered?: ")) SwapCaseAndCenter(a_string, upper_or_lower, center_num) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
#This is the actual code: def SwapCaseAndCenter(a_string, upper_or_lower = None): if upper_or_lower == "upper": print a_string.center(center_num).upper() if upper_or_lower == "lower": print a_string.center(center_num).lower() a_string = raw_input("Give me a word, or letter: ") upper_or_lower = raw_input("upper, or lower character(s): ") center_num = int(raw_input("Where should number be centered?: ")) SwapCaseAndCenter(a_string, upper_or_lower) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
Algorithm it, and look at the instance below the function first where variables are drawn in as raw input, and comments with # are just comments, not part of the code: def SwapCaseAndCenter(a_string, upper_or_lower = None): #find if it's upper, and print if upper_or_lower == "upper": print a_string.center(center_num).upper() #find if it's upper, and print if upper_or_lower == "lower": print a_string.center(center_num).lower() #Instance of function SwapCaseAndCenter vars input #define the string to be upper/lower cased and centered at a particular point a_string = raw_input("Give me a word, or letter: ") #define if it's upper or lower upper_or_lower = raw_input("upper, or lower character(s): ") #define where it should be centered with an int cast center_num = int(raw_input("Where should number be centered?: ")) #call the function with the params, and look above to the function SwapCaseAndCenter(a_string, upper_or_lower) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
On 10/19/2012 11:40 AM, Daniel Gulko wrote: > Thanks David. This has been helpful in understanding a bit more on how > parameters are passed through. Please don't top-post. it ruins the sequence of events. Your comments above happened after the parts you quote below. So why are they not after the things they follow? There is a long-standing convention in this forum, and many others, and why let Microsoft ruin it for all of us? >> Date: Thu, 18 Oct 2012 04:44:55 -0400 >> Subject: Re: [Tutor] Help Passing Variables >> From: dwightdhu...@gmail.com >> To: dangu...@hotmail.com >> CC: tutor@python.org >> >> #A little more complex in terms of params: >> >> def SwapCaseAndCenter(*kwargs): By convention kwargs is used for keyword arguments, while the * means positional arguments. The function uses none of them, so it's all bogus. You can't learn anything useful about argument passing from this example code, except what NOT to do. >> if upper_or_lower == "upper": >> print a_string.center(center_num).upper() >> >> if upper_or_lower == "lower": >> print a_string.center(center_num).lower() >> >> To understand the most elementary aspects of function passing, let's write a whole new function, and call it a few times. Paste this into a file, and try various things, and make sure you see how they work. Or reread Alan's email, which was also correct and clear. First point, a simple function may take zero arguments, or one, or two, or fifty. We'll start with positional arguments, which means order matters. Python also supports keyword arguments, default values, and methods, none of which I'll cover here. When you define a function, you specify its formal parameters. You do that by putting the parameter names inside the parentheses. Unlike most languages, you do NOT specify the types of any of these. But the order matters, and the number matters. So if we define a function: def truncate_string(a_string, width): temp = a_string[:width] return temp That function needs to be called with two arguments, which match the two formal parameters. They do NOT have to have the same names, and in fact they might well be literal strings or ints, or variables with string and int values. print truncate_string("this is a long string", 4) should print out "this" (without the quotes, of course) name = raw_input("give me a name") trunc_name = truncate_string(name, 8) print trunc_name truncate_string("aaa") #gives error, because it has the wrong number of arguments Does this help? -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
Thanks David. This has been helpful in understanding a bit more on how parameters are passed through. > Date: Thu, 18 Oct 2012 04:44:55 -0400 > Subject: Re: [Tutor] Help Passing Variables > From: dwightdhu...@gmail.com > To: dangu...@hotmail.com > CC: tutor@python.org > > #A little more complex in terms of params: > > def SwapCaseAndCenter(*kwargs): > > if upper_or_lower == "upper": > print a_string.center(center_num).upper() > > if upper_or_lower == "lower": > print a_string.center(center_num).lower() > > a_string = raw_input("Give me a word, or letter: ") > upper_or_lower = raw_input("upper, or lower character(s): ") > center_num = int(raw_input("Where should number be centered?: ")) > SwapCaseAndCenter(a_string, upper_or_lower, center_num) > > > > -- > Best Regards, > David Hutto > CEO: http://www.hitwebdevelopment.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
On 10/18/2012 03:08 AM, Daniel Gulko wrote: > > > > Hi Python Tutor, I have a write a simple function named > "SwapCaseAndCenter(a_string, width). So why did you define it with only one formal parameter? > The idea is to use the function swapcase and center so that when the > userenters a string it centers it and swaps the case (e.g. upper to lower and > vice versa). The function calls for passing in two variables "a_string, > width" but I am still confused on this concept. In my code below I seem to > be able to pass in the variable "a_string" and found out how to use the > "center along with swapcase" functions. I am still unsure howto pass in the > variable "width". In my code I have hard coded the centering but I am not > sure if instead I use the variable width to determine the centering. Any > suggestions, help or examples of how to do this is appreciated. > def SwapCaseAndCenter(a_string):while True: As you can see, your email program thoroughly messed up this source code. Please send your messages in text form, as html frequently gets trashed as it goes through various gateways. This is a text-mailing list. > a_string=raw_input("give me a word: (enter to quit): ") > Why do you ask the user for the input, when it was already supplied as a parameter? it's best practice to separate input from calculation, and your teacher had done that in his/her specification. >if a_string: > print a_string.center(60).swapcase() > elif not a_string: > print "you did not provide a word. goodbye" Presumably you should be returning the centered/swapped string. No idea where the following break is supposed to happen. But once you eliminate the raw_input, you'll be eliminating the while True and the break. > break SwapCaseAndCenter(a_string) > Thanks, Dan -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Passing Variables
On 18/10/12 08:08, Daniel Gulko wrote: The function calls for passing in two variables "a_string, width" but I am still confused on this concept. You just provide the list of input parameters when you define the function: >>> def add(x,y): ... return x+y ... >>> add(4,5) 9 I define add to take two parameters x and y. I then supply two corresponding arguments, 4,5 when I call add. I then use x and y inside my function (x+y) like ordinary variables. am still unsure how to pass in the variable "width". add it to your function definition as I did for add() Then add a width value to the call to your function centering but I am not sure if instead I use the variable width to determine the centering. Yes, or being pedantic, you use the parameter 'width' in your call to centre() def SwapCaseAndCenter(a_string): while True: a_string=raw_input("give me a word: (enter to quit): ") if a_string: print a_string.center(60).swapcase() elif not a_string: print "you did not provide a word. goodbye" break BTW putting break here means your loop only ever executes once. Frankly I would take the loop out and prompt the user for a_string outside the function and pass the string in along with the width. I'd also return the modified string so the caller can print it. Also rather than use the elif line I'd change it to an else. The test is implied by the fact it failed the initial if test. Either a_string is True (it exists) or it is not True, there is no third option so you don't need a separate elif test. If you really want to prompt the user multiple times do that outside the function: while True: my_string=raw_input("give me a word: (enter to quit): ") if my_string: print SwapCaseAndCenter(my_string,60) else: print "you did not provide a word. goodbye" break HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help Passing Variables
Hi Python Tutor, I have a write a simple function named "SwapCaseAndCenter(a_string, width). The idea is to use the function swapcase and center so that when the userenters a string it centers it and swaps the case (e.g. upper to lower and vice versa). The function calls for passing in two variables "a_string, width" but I am still confused on this concept. In my code below I seem to be able to pass in the variable "a_string" and found out how to use the "center along with swapcase" functions. I am still unsure howto pass in the variable "width". In my code I have hard coded the centering but I am not sure if instead I use the variable width to determine the centering. Any suggestions, help or examples of how to do this is appreciated. def SwapCaseAndCenter(a_string):while True: a_string=raw_input("give me a word: (enter to quit): ") if a_string: print a_string.center(60).swapcase() elif not a_string: print "you did not provide a word. goodbye" break SwapCaseAndCenter(a_string) Thanks, Dan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor