[Tutor] Contents of Tutor digest, Help with hangman

Timo timomlists at gmail.com
Mon Jul 19 19:54:13 CEST 2010


On 19-07-10 18:20, John Palmer wrote:
> Thanks a lot for the help guys, but when I use the 
> getpass.getpass(Enter your word here, I get a different response to 
> what you get. This is what happen with mine:
>
> >>> import getpass
> >>> s = getpass.getpass("Enter your word here: ")
> Warning: Password input may be echoed.
> Please enter your secret word: hangman
>
> >>> s
> 'hangman'
> >>>
This may not be really helpful, but I am using Ubuntu 10.04 too and have 
no problems. Running in the Gnome terminal:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import getpass
 >>> s = getpass.getpass("Enter your word here: ")
Enter your word here:
 >>> print s
blah

Cheers,
Timo

>
>
> I'm guessing that its something to do with the "Warning: Password may 
> be echoed" line. In the documentation it says:
>
> "If echo free input is unavailable getpass() falls back to printing a 
> warning message to stream and reading
> from sys.stdin and issuing a GetPassWarning."
>
> But i'm not sure what that means, sry to be a pain, and again thanks 
> for all the help.
>
> I did manage to find another solution which is just to print a large 
> number of blank lines, which just moved the line with the word in it 
> off the screen, but I don't really like it to be honest. The getpass 
> module seems to be the best solution i just don't understand why its 
> not working for me.
>
> Regards
> John
>
>
>
> On 19 July 2010 16:02, <tutor-request at python.org 
> <mailto:tutor-request at python.org>> wrote:
>
>     Send Tutor mailing list submissions to
>     tutor at python.org <mailto:tutor at python.org>
>
>     To subscribe or unsubscribe via the World Wide Web, visit
>     http://mail.python.org/mailman/listinfo/tutor
>     or, via email, send a message with subject or body 'help' to
>     tutor-request at python.org <mailto:tutor-request at python.org>
>
>     You can reach the person managing the list at
>     tutor-owner at python.org <mailto:tutor-owner at python.org>
>
>     When replying, please edit your Subject line so it is more specific
>     than "Re: Contents of Tutor digest..."
>
>
>     Today's Topics:
>
>       1. Re: A file containing a string of 1 billion random digits.
>          (Peter Otten)
>       2. Re: A file containing a string of 1 billion random digits.
>          (ALAN GAULD)
>       3. Re: A file containing a string of 1 billion random digits.
>          (Richard D. Moores)
>       4. Re: A file containing a string of 1 billion random digits.
>          (Richard D. Moores)
>       5. Re: Contents of Tutor digest, help with Hangman program
>          (Steven D'Aprano)
>       6. Re: Contents of Tutor digest, help with Hangman program
>          (bob gailer)
>       7. Re: A file containing a string of 1 billion random digits.
>          (Steven D'Aprano)
>
>
>     ----------------------------------------------------------------------
>
>     Message: 1
>     Date: Mon, 19 Jul 2010 15:45:43 +0200
>     From: Peter Otten <__peter__ at web.de <mailto:peter__ at web.de>>
>     To: tutor at python.org <mailto:tutor at python.org>
>     Subject: Re: [Tutor] A file containing a string of 1 billion random
>            digits.
>     Message-ID: <i21ku0$e00$1 at dough.gmane.org <mailto:1 at dough.gmane.org>>
>     Content-Type: text/plain; charset="ISO-8859-1"
>
>     Richard D. Moores wrote:
>
>     > On Mon, Jul 19, 2010 at 04:51, Peter Otten <__peter__ at web.de
>     <mailto:peter__ at web.de>> wrote:
>     >> bob gailer wrote:
>     >>
>     >>> Check this out:
>     >>>
>     >>> import random, time
>     >>> s = time.time()
>     >>> cycles = 1000
>     >>> d = "0123456789"*100
>     >>> f = open("numbers.txt", "w")
>     >>> for i in xrange(n):
>     >>> l = []
>     >>> l.extend(random.sample(d, 1000))
>     >>> f.write(''.join(l))
>     >>> f.close()
>     >>> print time.time() - s
>     >>
>     >> Note that this is not random. E. g. the start sequence "0"*101
>     should
>     >> have a likelyhood of 1/10**101 but is impossible to generate
>     with your
>     >> setup.
>     > I not sure exactly what you mean, because I don't fully understand
>     > that '*' (despite Alan's patient explanation), but if you run
>     >
>     > import random
>     > cycles = 100000
>     > d = "0123456789"*10
>     > for i in range(cycles):
>     > l = []
>     > l.extend(random.sample(d, 100))
>     > s = (''.join(l))
>     > if s[:4] == '0101':
>     > print(s)
>     >
>     > You'll see a bunch of strings that begin with "0101"
>     >
>     > Or if you run
>     >
>     > import random
>     > cycles = 50
>     > d = "0123456789"*10
>     > for i in range(cycles):
>     > l = []
>     > l.extend(random.sample(d, 100))
>     > s = (''.join(l))
>     > if s[:1] == '0':
>     > print(s)
>     >
>     > You'll see some that begin with '0'.
>     >
>     > Am I on the right track?
>
>     No. If you fire up your python interpreter you can do
>
>     >>> "0"*10
>     '0000000000'
>
>     i. e. "0"*101 is a sequence of 101 zeros. Because a sample can
>     pick every
>     item in the population only once and there are only 100 zeros, at
>     most 100
>     of them can be drawn, and the more are drawn the less likely it
>     becomes that
>     another one is drawn. The simplest demo is probably
>
>     random.sample([0, 1], 2)
>
>     Possible returns are [0, 1] and [1, 0], but for true randomness
>     you want [1,
>     1] and [0, 0], too. The more often the items are repeated the less
>     pronounced that bias becomes, e. g.
>
>     random.sample([0, 1, 0, 1], 2)
>
>     can produce all combinations, but [0, 1] is twice as likely as [0, 0]
>     because once the first 0 is drawn there is only one 0 left, but
>     two 1s.
>     Here's a demonstration:
>
>     >>> from collections import defaultdict
>     >>> d = defaultdict(int)
>     >>> for i in range(1000):
>     ...     d[tuple(random.sample([0, 1]*2, 2))] += 1
>     ...
>     >>> dict(d)
>     {(0, 1): 333, (1, 0): 308, (0, 0): 174, (1, 1): 185}
>
>     Peter
>
>
>
>     ------------------------------
>
>     Message: 2
>     Date: Mon, 19 Jul 2010 07:14:18 -0700 (PDT)
>     From: ALAN GAULD <alan.gauld at btinternet.com
>     <mailto:alan.gauld at btinternet.com>>
>     To: "Richard D. Moores" <rdmoores at gmail.com
>     <mailto:rdmoores at gmail.com>>
>     Cc: tutor at python.org <mailto:tutor at python.org>
>     Subject: Re: [Tutor] A file containing a string of 1 billion random
>            digits.
>     Message-ID: <94846.12586.qm at web86706.mail.ird.yahoo.com
>     <mailto:94846.12586.qm at web86706.mail.ird.yahoo.com>>
>     Content-Type: text/plain; charset=utf-8
>
>
>
>     > 4 and executed many times. Seems the 0 in 0dddd is
>     > there when a dddd is a 3-digit number such as 123.
>     > In that case a zero is prefixed to 123 to produce
>     > 0123. Or if just 23, 2 zeros are prefixed, etc.
>     > Correct?
>
>     Yes, the zero indicates that the string should be padded
>     with zeros to the length specified. The format string
>     documentation gives all the details but while zero
>     padding is fairly common the asterisk is less so, that's
>     why I explained it but not the zero...I assumed it was
>     the asterisk that was confusing you...
>
>     HTH,
>
>     Alan G.
>
>
>
>     ------------------------------
>
>     Message: 3
>     Date: Mon, 19 Jul 2010 07:14:13 -0700
>     From: "Richard D. Moores" <rdmoores at gmail.com
>     <mailto:rdmoores at gmail.com>>
>     To: Peter Otten <__peter__ at web.de <mailto:peter__ at web.de>>
>     Cc: tutor at python.org <mailto:tutor at python.org>
>     Subject: Re: [Tutor] A file containing a string of 1 billion random
>            digits.
>     Message-ID:
>     <AANLkTikctB03vyzHhhvmpg8Hrf6lMhRDIGbtKcoNPcn- at mail.gmail.com
>     <mailto:AANLkTikctB03vyzHhhvmpg8Hrf6lMhRDIGbtKcoNPcn- at mail.gmail.com>>
>     Content-Type: text/plain; charset=UTF-8
>
>     On Mon, Jul 19, 2010 at 06:45, Peter Otten <__peter__ at web.de
>     <mailto:peter__ at web.de>> wrote:
>
>     > No. If you fire up your python interpreter you can do
>     >
>     >>>> "0"*10
>     > '0000000000'
>
>     Ah, you're absolutely right. Sorry, I misunderstood you and your '*'.
>     Good catch.
>
>     Dick
>
>
>     ------------------------------
>
>     Message: 4
>     Date: Mon, 19 Jul 2010 07:48:13 -0700
>     From: "Richard D. Moores" <rdmoores at gmail.com
>     <mailto:rdmoores at gmail.com>>
>     To: ALAN GAULD <alan.gauld at btinternet.com
>     <mailto:alan.gauld at btinternet.com>>
>     Cc: tutor at python.org <mailto:tutor at python.org>
>     Subject: Re: [Tutor] A file containing a string of 1 billion random
>            digits.
>     Message-ID:
>     <AANLkTinCzLYCJ6aOfPo64KFUV1UeAcnE7bvMhvutZsh_ at mail.gmail.com
>     <mailto:AANLkTinCzLYCJ6aOfPo64KFUV1UeAcnE7bvMhvutZsh_ at mail.gmail.com>>
>     Content-Type: text/plain; charset=UTF-8
>
>     On Mon, Jul 19, 2010 at 07:14, ALAN GAULD
>     <alan.gauld at btinternet.com <mailto:alan.gauld at btinternet.com>> wrote:
>     >
>     >
>     >> 4 and executed many times. Seems the 0 in 0dddd is
>     >> there when a dddd is a 3-digit number such as 123.
>     >> In that case a zero is prefixed to 123 to produce
>     >> 0123. Or if just 23, 2 zeros are prefixed, etc.
>     >> Correct?
>     >
>     > Yes, the zero indicates that the string should be padded
>     > with zeros to the length specified. The format string
>     > documentation gives all the details
>
>     I've been unable to find any mention of that use of the asterisk in
>     the 3.1 docs, in
>
>     http://docs.python.org/py3k/library/string.html#formatspec
>
>     or
>
>     http://docs.python.org/py3k/library/string.html#formatstrings
>
>     Suggestion?
>
>     Dick
>
>
>     ------------------------------
>
>     Message: 5
>     Date: Tue, 20 Jul 2010 00:54:57 +1000
>     From: Steven D'Aprano <steve at pearwood.info
>     <mailto:steve at pearwood.info>>
>     To: tutor at python.org <mailto:tutor at python.org>
>     Subject: Re: [Tutor] Contents of Tutor digest, help with Hangman
>            program
>     Message-ID: <201007200054.57927.steve at pearwood.info
>     <mailto:201007200054.57927.steve at pearwood.info>>
>     Content-Type: text/plain;  charset="utf-8"
>
>     On Mon, 19 Jul 2010 09:37:25 pm John Palmer wrote:
>     > Hi Alan thanks for the help. I did try the getpass module, I think I
>     > used:
>     >
>     > getpass.getpass()
>     >
>     > This actually prompted the user to enter a password, which isn't
>     > really what I want. Unless there's something i'm missing with this
>     > module? I'll take another look anyway.
>
>     Tell the function what prompt to use:
>
>     >>> import getpass
>     >>> s = getpass.getpass("Please enter your secret word: ")
>     Please enter your secret word:
>     >>>
>     >>> print s
>     anti-disestablishmentarianism
>
>
>
>     --
>     Steven D'Aprano
>
>
>     ------------------------------
>
>     Message: 6
>     Date: Mon, 19 Jul 2010 10:57:11 -0400
>     From: bob gailer <bgailer at gmail.com <mailto:bgailer at gmail.com>>
>     To: John Palmer <speederpython at gmail.com
>     <mailto:speederpython at gmail.com>>
>     Cc: tutor at python.org <mailto:tutor at python.org>
>     Subject: Re: [Tutor] Contents of Tutor digest, help with Hangman
>            program
>     Message-ID: <4C4467C7.1060701 at gmail.com
>     <mailto:4C4467C7.1060701 at gmail.com>>
>     Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
>
>     On 7/19/2010 7:37 AM, John Palmer wrote:
>     > Hi Alan thanks for the help. I did try the getpass module, I think I
>     > used:
>     >
>     > getpass.getpass()
>     >
>     > This actually prompted the user to enter a password, which isn't
>     > really what I want. Unless there's something i'm missing with this
>     > module? I'll take another look anyway.
>
>     Reading the documentation (15.7 in Python 3):
>
>     The getpass module provides two functions:
>
>     getpass.getpass(/prompt='Password: '/, /stream=None/)?
>     <http://docs.python.org/py3k/library/getpass.html?highlight=getpass#getpass.getpass>
>
>        Prompt the user for a password without echoing. The user is
>     prompted
>        using the string /prompt/, which defaults to 'Password: '.
>
>     HTH
>
>     --
>     Bob Gailer
>     919-636-4239
>     Chapel Hill NC
>
>     -------------- next part --------------
>     An HTML attachment was scrubbed...
>     URL:
>     <http://mail.python.org/pipermail/tutor/attachments/20100719/20256873/attachment-0001.html>
>
>     ------------------------------
>
>     Message: 7
>     Date: Tue, 20 Jul 2010 01:01:58 +1000
>     From: Steven D'Aprano <steve at pearwood.info
>     <mailto:steve at pearwood.info>>
>     To: tutor at python.org <mailto:tutor at python.org>
>     Subject: Re: [Tutor] A file containing a string of 1 billion random
>            digits.
>     Message-ID: <201007200101.58268.steve at pearwood.info
>     <mailto:201007200101.58268.steve at pearwood.info>>
>     Content-Type: text/plain;  charset="iso-8859-1"
>
>     On Tue, 20 Jul 2010 12:48:13 am Richard D. Moores wrote:
>     > On Mon, Jul 19, 2010 at 07:14, ALAN GAULD
>     <alan.gauld at btinternet.com <mailto:alan.gauld at btinternet.com>>
>     wrote:
>     > >> 4 and executed many times. Seems the 0 in 0dddd is
>     > >> there when a dddd is a 3-digit number such as 123.
>     > >> In that case a zero is prefixed to 123 to produce
>     > >> 0123. Or if just 23, 2 zeros are prefixed, etc.
>     > >> Correct?
>     > >
>     > > Yes, the zero indicates that the string should be padded
>     > > with zeros to the length specified. The format string
>     > > documentation gives all the details
>     >
>     > I've been unable to find any mention of that use of the asterisk in
>     > the 3.1 docs, in
>     >
>     > http://docs.python.org/py3k/library/string.html#formatspec
>     >
>     > or
>     >
>     > http://docs.python.org/py3k/library/string.html#formatstrings
>     >
>     > Suggestion?
>
>     You're looking in the wrong place. This is not part of format strings,
>     as it doesn't use the str.format() method. It uses the % string
>     interpolation operator.
>
>     http://docs.python.org/py3k/library/stdtypes.html#old-string-formatting-operations
>
>
>     You can get the same result with the format mini-language. See the
>     example "Nested arguments and more complex examples" just before the
>     section on Template Strings here:
>
>     http://docs.python.org/py3k/library/string.html#format-specification-mini-language
>
>
>
>
>     --
>     Steven D'Aprano
>
>
>     ------------------------------
>
>     _______________________________________________
>     Tutor maillist  - Tutor at python.org <mailto:Tutor at python.org>
>     http://mail.python.org/mailman/listinfo/tutor
>
>
>     End of Tutor Digest, Vol 77, Issue 70
>     *************************************
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>    



More information about the Tutor mailing list