Re: [Tutor] need help with python for counter

2012-12-19 Thread bob gailer

On 12/19/2012 12:40 AM, Brandon Merritt wrote:
I feel silly, but I'm having the darndest time trying to figure out 
why this for counter won't work. I know that there is the count method 
for the string class, but I was just trying to do it the syntactical 
way to prove myself that I know the very basics. As of right now, my 
script is just returning 1 or 0, even if I very clearly make sure that 
I specify at least 4 instances of the digit in my number string:


number = raw_input('Enter a 7-unit number: ')

digit = raw_input('Enter a single digit: ')

for i in number:
count = 0
if i == digit:
count += 1

Drop next two statements and try agan

else:
count = 0

print count


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] need help with python for counter

2012-12-19 Thread Alan Gauld

On 19/12/12 05:40, Brandon Merritt wrote:


the string class, but I was just trying to do it the syntactical way to
prove myself that I know the very basics.


Which is not a bad thing.


just returning 1 or 0, even if I very clearly make sure that I specify
at least 4 instances of the digit in my number string:

number = raw_input('Enter a 7-unit number: ')

digit = raw_input('Enter a single digit: ')

for i in number:
 count = 0


you reset count to zero everytime round the loop.
You need to move this outside the loop.


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


Re: [Tutor] need help with python for counter

2012-12-19 Thread Dave Angel
On 12/19/2012 12:40 AM, Brandon Merritt wrote:
> I feel silly, but I'm having the darndest time trying to figure out why
> this for counter won't work. I know that there is the count method for the
> string class, but I was just trying to do it the syntactical way to prove
> myself that I know the very basics. As of right now, my script is just
> returning 1 or 0, even if I very clearly make sure that I specify at least
> 4 instances of the digit in my number string:
>
> number = raw_input('Enter a 7-unit number: ')
>
> digit = raw_input('Enter a single digit: ')
>
> for i in number:
> count = 0
> if i == digit:
> count += 1
> else:
> count = 0
>
> print count
>
>

Two separate problems in that code, either of which would be enough to
ruin the count value.

1) Your code sets the count to zero INSIDE the loop, so every time
through, you trash whatever earlier value you had.  This assures that
only the last loop matters!

You ought to be zeroing the counter just once, before the loop starts.

2) In your else clause, as soon as you encounter some other character,
the code is zeroing the counter.

count = 0

for i in number:
if i == digit:
count += 1

While I've got your attention, let me point out that nowhere in your
description do you actually tell us what values you're entering, and
what values you expect.  You ought to supply sample values which
illustrate the problem.  The above code is really only a guess, as to
what you wanted.

But the notion of doing the initialization OUTSIDE the loop is an
important one, and I'm sure that part is correct.

-- 

DaveA

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


Re: [Tutor] need help with python for counter

2012-12-18 Thread शंतनू

On 19/12/12 5:12 PM, Mitya Sirenef wrote:
> You can also do:
>
> count = sum(i==digit for i in number)
>
> I think this is very clear and short..

Another...

import re
count = len(re.findall(digit, number))

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


Re: [Tutor] need help with python for counter

2012-12-18 Thread DoanVietTrungAtGmail
After incrementing for a little while, if the condition i == digit is not
met for the current character, count is reset by the else: branch. Your
count variable must feel frustrated like a dog which keeps being yanked
back by a cord of length 0 when it tries to get free.

Trung Doan
==

On Wed, Dec 19, 2012 at 4:40 PM, Brandon Merritt wrote:

> I feel silly, but I'm having the darndest time trying to figure out why
> this for counter won't work. I know that there is the count method for the
> string class, but I was just trying to do it the syntactical way to prove
> myself that I know the very basics. As of right now, my script is just
> returning 1 or 0, even if I very clearly make sure that I specify at least
> 4 instances of the digit in my number string:
>
> number = raw_input('Enter a 7-unit number: ')
>
> digit = raw_input('Enter a single digit: ')
>
> for i in number:
> count = 0
> if i == digit:
> count += 1
> else:
> count = 0
>
> print count
>
> Thanks,
> Brandon
>
>
> --
> *Brandon Merritt**
> (707) 481-1744*
> *
> *
>
> ___
> 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


Re: [Tutor] need help with python for counter

2012-12-18 Thread Mitya Sirenef

On 12/19/2012 12:40 AM, Brandon Merritt wrote:
I feel silly, but I'm having the darndest time trying to figure out 
why this for counter won't work. I know that there is the count method 
for the string class, but I was just trying to do it the syntactical 
way to prove myself that I know the very basics. As of right now, my 
script is just returning 1 or 0, even if I very clearly make sure that 
I specify at least 4 instances of the digit in my number string:


number = raw_input('Enter a 7-unit number: ')

digit = raw_input('Enter a single digit: ')

for i in number:
count = 0
if i == digit:
count += 1
else:
count = 0



Why do you have the else: clause?

You can also do:

count = sum(i==digit for i in number)

I think this is very clear and short..

 -m



--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

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


[Tutor] need help with python for counter

2012-12-18 Thread Brandon Merritt
I feel silly, but I'm having the darndest time trying to figure out why
this for counter won't work. I know that there is the count method for the
string class, but I was just trying to do it the syntactical way to prove
myself that I know the very basics. As of right now, my script is just
returning 1 or 0, even if I very clearly make sure that I specify at least
4 instances of the digit in my number string:

number = raw_input('Enter a 7-unit number: ')

digit = raw_input('Enter a single digit: ')

for i in number:
count = 0
if i == digit:
count += 1
else:
count = 0

print count

Thanks,
Brandon


-- 
*Brandon Merritt**
(707) 481-1744*
*
*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor