Re: [Tutor] creating the equivalent of string.strip()

2007-10-01 Thread Andrew James
I've gone ahead and created a script that does this, however it also 
strips punctuation. I was originally just comparing each character to a 
string containing a single space ' ' but even using s[-1].isspace() I 
lose punctuation marks. Any idea why that's happening?

(Not the OP, I just thought this would be interesting to do)

Bob Nienhuis wrote:
> BTW, GMail brings up some interesting sponsored links when the subject 
> line
> has string.strip in it ( :-0)
>
> On 9/30/07, *Alan Gauld* < [EMAIL PROTECTED] 
> > wrote:
>
>
> "wesley chun" < [EMAIL PROTECTED] > wrote
> in message
> news:[EMAIL PROTECTED]
> >> > I'm not sure how to proceed.  My biggest stumbling
> >> > block is how to detect the leading and trailing
> >> > whitespace.
> >>
> >> Use indexing.
> >> Try using a while loop.
> >> Or maybe two?
> >>
> >> And strings have an isspace method...
> >
> > unfortunately, the isspace() string method only returns True if all
> > chars in the string are whitespace chars and False otherwise, so
> > that's a bummer.
>
> But the string can be one character long:
>
> s = 'fred\n'
> print s[-1].isspace()  # --> True
>
> :-)
>
> Alan G
>
>
> ___
> Tutor maillist  -  Tutor@python.org 
> http://mail.python.org/mailman/listinfo/tutor
>
>
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   
> 
>
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.488 / Virus Database: 269.13.35 - Release Date: 29/09/2007 12:00 
> AM
>   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating the equivalent of string.strip()

2007-10-02 Thread Andrew James
Helps if I send it to the group...

And Kent, I didn't post it originally because I figured the other guy 
was still working on his script. Besides, I didn't think it'd be that 
far fetched for you to assume I was using something like "while s[0] == 
' ':"

Andrew James wrote:
> I'm always nervous about sharing my code... It's like being back in 
> high school, I don't want the other kids to laugh at me. Anyway, 
> running that in IDLE with a string such as '  
> testing.  ' Will print 'testing' at the 
> end of the first (second, really) loop.
>
> def strip(s):
>while s[0].isspace() == True:
>while s[-1].isspace() == True:
>s = s[:-2]
>print s
>s = s[1:]
>print s
>
> test = raw_input('Enter a string with plenty of leading and trailing 
> whitespace:')
>
> strip(test)
>
> Alan Gauld wrote:
>> "Andrew James" <[EMAIL PROTECTED]> wrote
>>
>>  
>>> string containing a single space ' ' but even using s[-1].isspace() I
>>> lose punctuation marks. Any idea why that's happening?
>>> 
>>
>> care to show us what you are doing?
>>
>>  
>>>>> ';'.isspace()
>>>>> 
>> False
>>
>> So puntuation should not show up as true...
>>
>> Alan G.
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>>   
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating the equivalent of string.strip()

2007-10-02 Thread Andrew James
And as soon as I send it again I realise a pretty stupid error. The two 
loops shouldn't be nested.

Andrew James wrote:
> Helps if I send it to the group...
>
> And Kent, I didn't post it originally because I figured the other guy 
> was still working on his script. Besides, I didn't think it'd be that 
> far fetched for you to assume I was using something like "while s[0] 
> == ' ':"
>
> Andrew James wrote:
>> I'm always nervous about sharing my code... It's like being back in 
>> high school, I don't want the other kids to laugh at me. Anyway, 
>> running that in IDLE with a string such as '  
>> testing.  ' Will print 'testing' at the 
>> end of the first (second, really) loop.
>>
>> def strip(s):
>>while s[0].isspace() == True:
>>while s[-1].isspace() == True:
>>s = s[:-2]
>>print s
>>    s = s[1:]
>>print s
>>
>> test = raw_input('Enter a string with plenty of leading and trailing 
>> whitespace:')
>>
>> strip(test)
>>
>> Alan Gauld wrote:
>>> "Andrew James" <[EMAIL PROTECTED]> wrote
>>>
>>>  
>>>> string containing a single space ' ' but even using s[-1].isspace() I
>>>> lose punctuation marks. Any idea why that's happening?
>>>> 
>>>
>>> care to show us what you are doing?
>>>
>>>  
>>>>>> ';'.isspace()
>>>>>> 
>>> False
>>>
>>> So puntuation should not show up as true...
>>>
>>> Alan G.
>>>
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>>   
>>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating the equivalent of string.strip()

2007-10-02 Thread Andrew James
*headbang*

I'm an idiot. s=s[:-2] should be s=s[:-1].

Andrew James wrote:
> And as soon as I send it again I realise a pretty stupid error. The 
> two loops shouldn't be nested.
>
> Andrew James wrote:
>> Helps if I send it to the group...
>>
>> And Kent, I didn't post it originally because I figured the other guy 
>> was still working on his script. Besides, I didn't think it'd be that 
>> far fetched for you to assume I was using something like "while s[0] 
>> == ' ':"
>>
>> Andrew James wrote:
>>> I'm always nervous about sharing my code... It's like being back in 
>>> high school, I don't want the other kids to laugh at me. Anyway, 
>>> running that in IDLE with a string such as '  
>>> testing.  ' Will print 'testing' at the 
>>> end of the first (second, really) loop.
>>>
>>> def strip(s):
>>>while s[0].isspace() == True:
>>>while s[-1].isspace() == True:
>>>    s = s[:-2]
>>>print s
>>>s = s[1:]
>>>print s
>>>
>>> test = raw_input('Enter a string with plenty of leading and trailing 
>>> whitespace:')
>>>
>>> strip(test)
>>>
>>> Alan Gauld wrote:
>>>> "Andrew James" <[EMAIL PROTECTED]> wrote
>>>>
>>>>  
>>>>> string containing a single space ' ' but even using s[-1].isspace() I
>>>>> lose punctuation marks. Any idea why that's happening?
>>>>> 
>>>>
>>>> care to show us what you are doing?
>>>>
>>>>  
>>>>>>> ';'.isspace()
>>>>>>> 
>>>> False
>>>>
>>>> So puntuation should not show up as true...
>>>>
>>>> Alan G.
>>>>
>>>>
>>>> ___
>>>> Tutor maillist  -  Tutor@python.org
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>>
>>>>   
>>>
>>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] random number generator

2007-10-04 Thread Andrew James
I need to start using the reply all button...

Andrew James wrote:
> while guess != number:
>guess = float(raw_input("Make another guess: "))
>if guess > number:
>print "Lower..."
>elif guess < number:
>print "Higher..."
>tries += 1
>
> You're asking people to change their choice before the while loop 
> ends. Now this looks a little ugly as it will be asking people to make 
> two guesses right off the bat, or it'll use the term "another" for 
> their first guess. Shouldn't be too hard to change. Or do this.
>
> while guess != number:
>if guess > number:
>print "Lower..."
>elif guess < number:
>print "Higher..."
>tries += 1
>if guess != number:
>guess = float(raw_input("Make another guess: "))
>
>
>
>
> Jerry VanBrimmer wrote:
>> I'm no Python wizard, I'm still learning myself. But I think you need
>> another "if" statement to check if "guess" is equal to "number".
>>
>> if guess == number:
>> print "Congratulations!"
>>
>>
>> Something like that.
>>
>>
>>
>> On 10/4/07, Jim Hutchinson <[EMAIL PROTECTED]> wrote:
>>  
>>> Hello,
>>>
>>> I am writing a little program to test a theory and as part of teaching
>>> myself Python. I've only been at this about a week now. I have a
>>> program that "should" work but doesn't. It generates a random number
>>> between 1 and 2 out to 10 decimal places. I think there is something
>>> wrong with how my random number is generated or defined or how my
>>> guesses are defined. I added a line to tell me what the random number
>>> is and then if I enter it as a guess it doesn't match and exit the
>>> loop. Any idea what I'm doing wrong? Here is a sample output:
>>>
>>> ---
>>> I'm thinking out to 10 decimal places. Good luck.
>>>
>>> 1.14981949962
>>> Make a guess: 1.14981949962
>>> Higher...
>>> Make another guess: 1.14981949963
>>> Lower...
>>> 1.14981949963
>>> Make another guess:
>>> ---
>>>
>>> Here is my code:
>>>
>>> ---
>>> # Number guessing game
>>> #
>>> # The computer will choose a number between 1 and 2 (to ten decimal 
>>> places)
>>> # and the player will try to guess the number. The program will tell 
>>> the
>>> # player the number is either higher or lower than the number they 
>>> guessed.
>>> import random
>>> import os
>>> os.system("clear")
>>> print "\nWelcome to 'Guess My Number'!"
>>> print "\nI'm thinking of a number between 1 and 2."
>>> print "\nYes, that's right. Between 1 and 2."
>>> print "\nYou have heard of decimals right? Well, I'm"
>>> print "\nthinking out to 10 decimal places. Good luck.\n"
>>> # set random value
>>> random.seed()
>>> number = random.random() + 1
>>> print number
>>> guess = float(raw_input("Make a guess: "))
>>> tries = 1
>>> # the guess loop
>>> while (guess != number):
>>> if (guess > number):
>>> print "Lower..."
>>> else:
>>> print "Higher..."
>>> guess = float(raw_input("Make another guess: "))
>>> tries += 1
>>> print "Congratulations! You guessed my number! The number was", number
>>> print "It took you only", tries, "tries!\n"
>>> # end
>>> ---
>>>
>>> Thanks,
>>> Jim
>>>
>>> -- 
>>> Please avoid sending me Word or PowerPoint attachments.
>>> See http://www.gnu.org/philosophy/no-word-attachments.html
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>> 
>>
>>
>>   
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] random number generator

2007-10-06 Thread Andrew James
I'm tempted to suggest using a hack to avoid floating point errors.

Anyway, it isn't choosing a number to ten decimal places. It's actually 
out to 11 in both examples you gave. And it evaluates to correct because 
your guesses were to at least 10 places, which is as far as you account 
for in the line 'while (abs(number-guess) > 0.01): '

Jim Hutchinson wrote:
> Greetings all,
>
> It seems I forgot to subscribe to the list so I didn't receive any of
> the replies. However, I did check the archive and found all of the
> very helpful suggestions. Thanks for your time.
>
> Based on the replies I was able to get this to more or less work.
> However, one problem still exists. If you guess a number that is
> slightly larger than the random number it evaluates to correct
>
> For example:
>
> 
> The number is 1.78889511441
> Make a guess: 1.788895114455
>
> Congratulations! You guessed my number! The number was 1.78889511441
>
> It took you only 1 tries!
> ---
>
> Or even:
>
> ---
> 1.36344601965
> Make a guess: 1.3634460196
>
> Congratulations! You guessed my number! The number was 1.36344601965
>
> It took you only 1 tries!
> ---
>
> I'm also unclear as to how it is choosing a random number out to 10
> decimal places. Is that the default and it's just coincidence that I
> chose 10? What if I want a random number to 20 decimal places or five?
>
> Here is the code:
>
> # Number guessing game
> #
> # The computer will choose a number between 1 and 2 (to ten decimal places)
> # and the player will try to guess the number. The program will tell the
> # player the number is either higher or lower than the number they guessed.
> import random
> import os
> os.system("clear")
> print "\nWelcome to 'Guess My Number'!"
> print "\nI'm thinking of a number between 1 and 2."
> print "\nYes, that's right. Between 1 and 2."
> print "\nYou have heard of decimals right? Well, I'm"
> print "\nthinking out to 10 decimal places. Good luck.\n"
> # set random value
> random.seed()
> number = random.random() + 1
> print number
> guess = float(raw_input("Make a guess: "))
> tries = 1
> # the guess loop
> while (abs(number-guess) > 0.01):
> if guess > number:
> print "Lower..."
> elif guess < number:
> print "Higher..."
> tries += 1
> if guess != number:
> guess = float(raw_input("Make another guess: "))
> print "\nCongratulations! You guessed my number! The number was", number
> print "\nIt took you only", tries, "tries!\n"
> # end
>
> Thanks again,
> Jim
>   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor