[Tutor] Need help with functions!

2010-01-25 Thread Raymond Kwok
Hello all,

I have started to learn Python a few months ago and I'm loving it! 
I
am creating a mini-game - the one that scrambles an English word and
asks you what the originalword should be, e.g. yhptno ---> python 

So,
I have a function called get_word() that does the random word thing and
returns two things - 1) original word and 2) scrambled word, namely
"originalword" and "jumble" respectively.  So every time I run the
get_word() function, it gives me a new word.  

The code is: 

jumble = get_word()[0]
originalword = get_word()[1]
print (jumble)
answer = input ("What do you think this word is? ")
while answer != originalword:  
    answer = input ("No! Guess again please.")
print ("\nThat's right! The word
 is",originalword)

The
while loop keeps running until the user guesses the right answer and it
will display the original word.  The thing is when it shows the
original word, it has in fact become a new word since the "get_word()"
function has been run again and returned a new word.  Does anyone know
how to solve this problem ? I hope it is not too confusing...

-Ray



  Yahoo!香港提供網上安全攻略,教你如何防範黑客! 請前往 http://hk.promo.yahoo.com/security/ 了解更多!___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The magic parentheses

2010-01-25 Thread Lie Ryan
Do you know python's object model? A lot of these things will make much
more sense once you do:
http://effbot.org/zone/python-objects.htm

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


[Tutor] email module, convert non multipart to multipart message

2010-01-25 Thread Bill Campbell
Is there a simple way to convert a MIMENonMultipart message parsed into a
MIMEMultipart message?

My goal is to take a message from the TouchTerm iPhone App with an openssh
public key that has been mangled by intervening mailers, and convert it
into a multipart message with the original text in one part and the cleaned
up key as an application/base64 part that can be easily saved by a non-
technical user.

I have written a routine that does this by creating a new multipart
container, copying all (well almost all) the headers into the new container
then attaching the original body and the clean key, but I think there ought
to be a neater way.

Thanks.

Bill
-- 
INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186  Skype: jwccsllc (206) 855-5792

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him.  -- Robert Heinlein
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The magic parentheses

2010-01-25 Thread Alan Gauld


"spir"  wrote 

Lie's example actually was:


a,b,c = 1,2,3
print (a,b,c) # here parenthesized

(1, 2, 3)


Oops, I've been using Python 3 too much, I mentally blanked 
out the parens!


Sorry about that.

Alan G.

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


Re: [Tutor] The magic parentheses

2010-01-25 Thread spir
On Mon, 25 Jan 2010 01:06:45 -
"Alan Gauld"  wrote:

> 
> "Lie Ryan"  wrote 
> 
> >> and used print, I thought they would be considered the same whether as 
> >> a variable, or as a direct line, guess not.
> > what is equivalent:
> > print (a, b, c)
> > 
> > and
> > x = a, b, c
> > print x
> > 
> > both construct a tuple and prints a,b,c as tuple
> 
> Not quite:
> 
> >>> a = 1
> >>> b = 2
> >>> c = 3
> >>> x = a,b,c
> >>> print a,b,c
> 1 2 3
> >>> print x
> (1, 2, 3)
> >>>
> 
> The first form prints multiple values the second prints the repr of a 
> single tuple value. The output is different.
> 
> Alan G.

Lie's example actually was:

>>> a,b,c = 1,2,3
>>> print (a,b,c)   # here parenthesized
(1, 2, 3)
>>> x = a,b,c
>>> print x
(1, 2, 3)

Both output values are tuples.

Denis


la vita e estrany

http://spir.wikidot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is it pythonesque

2010-01-25 Thread spir
On Sun, 24 Jan 2010 11:13:37 -0500
"Robert Berman"  wrote:

> Good morning,
> 
>  
> 
> Given the following code snippets:
> 
>  
> 
> def getuserinput():
> 
> while True:
> 
> s1 = raw_input('Enter fraction as N,D or 0,0 to exit>>')
> 
> delim = s1.find(',')
> 
> if delim < 0:
> 
> print 'invalid user input'
> 
> else:
> 
> n = int(s1[0:delim])
> 
> d = int(s1[delim+1::])
> 
> return n,d
> 
>  
> 
> def main():
> 
> while True:
> 
> n,d = getuserinput()
> 
> if n == 0 or d == 0: return 0
> 
>  
> 
> n and d are always returned provided the input is given as n,d. n/d will
> print an error message and the loop will reiterate until the user format is
> correct. Please note there is no true ending return for getuserinput() as it
> is hung off an if statement and if by some  chance it breaks, it should
> return None which will abort the program. While I suspect this style is
> devious, is it dangerous or 'wrong' to use. Comments and other renditions
> are most welcome.
> 
>  
> 
> Thank you,
> 
>  
> 
> Robert Berman

-0- Blank lines between statament is certainly not pythonesque ;-)
-1- Why not chose '/' as delimiter?
-2- You may check there is a single delimiter.
-3- You may consider a user-friendly (eg ''), rather than a programmer-friendly 
("0,0") code for breking the loop. It'd cost you a pair of lines of code.

For instance:

FRACT = '/'
def getUserInput():
while True:
s1 = raw_input('Enter fraction as N/D or enter to exit > 1')
if s1 == '':
return None# will trigger exit in main
nDelims = s1.count(FRACT)  # only 1 is valid
if nDelims == 1:
n,d = s1.split(FRACT)
return int(n),int(d)   # remaining issue
print 'invalid user input'
getUserInput()

An issue issue remains, namely that what around FRACT may not be valid integers.

Denis


la vita e estrany

http://spir.wikidot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python/Django issue

2010-01-25 Thread Hassan Baig
Dear List Members,

I hope you're well. I was wondering if you could help out in a basic query.
I'm a Facebook developer and I'm facing the following problem:

I have a flash file which calls up a url say http://test.com/createXML/ which
is caught and used up by a *python/django* code and it creates and redirects
to an XML. which is loaded by flash, to get values from the database.

The setup works fine when outside facebook, but as soon as I put the setup
in facebook, it stops loading the XML completely.

Any clues?

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


Re: [Tutor] The magic parentheses

2010-01-25 Thread Alan Gauld


"David Hutto"  wrote


>>> a = 1
>>> b = 2
>>> c = 3
>>> x = a,b,c
>>> print a,b,c
1 2 3
>>> print x
(1, 2, 3)


So 'print a,b,c' says display the values of a,b,c in the sequence of 
a,b,c given.
'print x' says print the value of x, x grabs a's value, b's value, and 
c's value,

and displays the values for print.


Not quite.
x is a tuple - a collection of values - it does not grab those values, it 
is given them
Once given them it keeps them. If we subsequently change 'a' that change 
will
not be reflected in x! (Note it gets more complex if 'a' is a mutable 
type.)



a = 1
b = 2
c = 3
x = a,b,c
print a,b,c

1 2 3

print x

(1, 2, 3)

a = 66
x

(1, 2, 3)

So no change in x even though a has changed.

print always just displays the string representation of the values passed 
to it.

So when passed a,b,c it prints the string version of a, the string version
of b and the string version of c. When passed x it prints the string 
version

of x which is a tuple. tuples in Python are represented as a sequence of
values inside parentheses. But x does not go and fetch the latest values
of a,b and c, it uses the values provided when x was created.


So the sequence becomes a tuple because it's a value of values,


x is a tuple because that's what we created when we assigned the 3 values.
Any sequence of values outside brackets or quotes is a tuple


t1 = 1,2,3
t2 = 'a','b','c'
t3 = (2,,4,6)
t4 = (t1,t2)



and it's sum comes back in the form of a tuple,


It was a tuple, it does not create itself dynamically.


not an actual end variable.


A tuple is an "end variable". It is just as much a valid type as an
integer, a string or a list. It is just another data type.


In other words the second leap to find the values of a,b,c for value x,


There is no second leap. It stores and uses the values passed to it in
the original assignment.


Now if I want to turn the first values listed under x into tuples
(or use the format function that I think was named in another
reply to return the non-tuple version of the value for x and
I'm going to try it in a few min) of the other two variables,


I'm not sure what you mean here.
The values in x are the original values of a,b and c.
You can access those values using x[0],x[1],x[2].
Is that what you mean?


g = f
a = b,c


This will fail because b and c don;t exist yet.


b = a,c


this will fail because c does not exist yet


c = a,g
x = a,b,c




This is off the topic I think, so I'll repost under another if necessary,
but how do I avoid going into the loop of tuples/variables.


There is no loop, it is not a dynamic lookup. The tuple stores the
values at the time of creation. If the values don;t exist yet(as above)
the assignment will fail to create a tuple and you will get an error.


If a is dependent on knowing what b,c are, and b is dependent
on knowing what a and c are, and c is dependent on knowing
what a and b are, how do I prevent from going into a defining
loop for each.


Python won;t let you create variables using other variables that
have not been defined yet. It is your responsibility as the programmer
to define valriables  before using them.


This is just basically playing with Python's functions, I was
just wondering how to approach such a problem where variables
must come in order but might be dependent on a following variable


It will fail. You must create the variables before use.
We, can do some clever things with functions that can defer
the evaluation of variables until later in the program but thats
an advanced topic that I won't go into here!


In the above, if I want to know c it requires knowing a,g. But
in order to know a, a needs to know b,c-so I can't list c
above a or a above c, because I get the error that the
value of the other is not known when I try to run the script.


Correct, you must find a way to order them such that the
dependencies are sorted out before assignment.

Take a look at the sections on variables and collections,
especially tuples, in the Raw Materials topic of my tutor for
more information..


HTH,

--
Alan Gauld
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