[Tutor] Help Passing Variables

2012-10-18 Thread Daniel Gulko




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


Re: [Tutor] Help Passing Variables

2012-10-19 Thread Daniel Gulko

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] Objects, object references, object values and memory addresses

2012-10-19 Thread Daniel Gulko

Great explanation Alan. I am a newbie at Python but descriptions like this 
really help me better understand.

Thanks again.

> To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Thu, 18 Oct 2012 08:32:41 +0100
> Subject: Re: [Tutor] Objects, object references,  object values and 
> memory addresses
> 
> On 18/10/12 04:41, boB Stepp wrote:
> >  From Programming in Python 3, 2nd edition (p. 22-23):
> >
>  a = ["Retention", 3, None]
>  b = ["Retention", 3, None]
>  a is b
> > False
>  b = a
>  a is b
> > True
> >
> > My current understanding is as follows: On the first two lines, two
> > separate objects are defined, stored in two separate blocks of memory.
> 
> Two separate list objects are created. In Python it is rarely helpful to 
> think about memory usage. Python is abstracted so far from the physical 
> machine that the connection usually depends on the creator of the 
> interpreter rather than the language.
> 
> > These two objects just happen to have the same value, ["Retention", 3,
> > None], stored in two separate locations.
> 
> Yes, but note that its the fact that they are two separate lists that 
> matters. Even if the contents were the same objects they would still be 
> two lists:
> 
>  >>> x = [42]
>  >>> y = 'spam'
>  >>> z = None
>  >>> a = [x,y,z]
>  >>> b = [x,y,z]   # different list, exact same content
>  >>> a is b
> False
>  >>> a == b
> True
>  >>> a = b
>  >>> a is b
> True
>  >>> a == b
> True
>  >>>
> 
>  > a and b, object references (Variables are what I used to call these.
> 
> And most folks still do...
> 
> > I ask: Which implementations of Python do this? In trying to make any
> > code I write portable across as many platforms as possible, should I
> > avoid using the identity operator, is (and its opposite, is not),
> > except when I wish to compare to None?
> 
> The point is that you don't know. And, even if you did, the very next 
> release might change it so you cannot rely on it. That's the real 
> message - do not assume a particular implementation technique because it 
> is not guaranteed to be that way or to stay that way.
> 
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help - Using Sort and Join

2012-10-22 Thread Daniel Gulko

Hi Python Tutor,

I have an issue trying to figure out how to print out final answers using sort 
and join functions.

Assignment Specification:
make a function that is a magic eight ball emulator. emulator will be a 
function that returns one of the possible answers. Make another function that 
runs the emulator over and over again until user wants to quit, printing answer 
each time. When user quits, present all answers that the user got again, but 
present them in alphabetical order. Use the join() function available for 
strings for this.

Below is an example output from my code:

ask a question (or press 'enter' to quit): will I be rich
You may rely on it.

ask a question (or press 'enter' to quit): will I be poor
It is decidedly so.

ask a question (or press 'enter' to quit): why is that
Better not tell you now.

As my code is written when user presses enter I simply break and print this 
current output:
Have a nice day


What I want is when user exits for my code to take all previous answers, 
joining them and sorting them like below:
ask a question (or press 'enter' to quit):
You may rely on it. It is decidedly so. Better not tell you now.

My code works right now but I am missing the final part in how to join and sort 
all answers. Any suggestions or examples is helpful as I 
am very new with Python.

Current code is attached for viewing.

Thanks,

Dan

  import random

def AskMagicEightBall():

answers = ("As I see it, yes.", 
   "It is certain.", 
   "It is decidedly so.", 
   "Most likely.", 
   "Outlook good.", 
   "Signs point to yes.", 
   "Without a doubt.", 
   "Yes.", 
   "Yes – definitely.", 
   "You may rely on it.",
   "Reply hazy, try again.", 
   "Ask again later.", 
   "Better not tell you now.", 
   "Do not count on it.", 
   "My reply is no.", 
   "My sources say no.", 
   "Outlook not so good.", 
   "Very doubtful.")

return random.choice(answers)

def RunEmulator():

while True:
question = raw_input("ask a question (or press 'enter' to quit): ")
if question:
answers=AskMagicEightBall()
print answers
elif not question:
print "Have a nice day"
break

RunEmulator()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor