Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with code
#1
Hi,
Here is a code:
def beginning_zeros(x):
    count = 0
    for letters in x:
        if letters == '0':
                  count += 1
         else: 
             return count
I wrote this code to count the zeros at the beginning. While i excecuted it for '0001' it gave the right answer(3) but when i ran for '0000' it gives me 1. Can someone explain why? And what should i do for this?
Reply
#2
Hi sarata,
Are you sure it returns 1 for the string '0000'? With me it returns 'None'.
And this is because you only return a value when a letter is encountered that is not '0'. This condition does not occur when you test '0000'.
The solution is to also return count when the for loop finishes.
Or, what I like more: break the loop when a letter is found that is not '0' and only have one return in the function.
def beginning_zeros(x):
    count = 0
    for letters in x:
        if letters == '0':
            count += 1
        else:
            break
    return count

print(beginning_zeros("0001"))
print(beginning_zeros("0000"))
Output:
3 4
Reply
#3
(Mar-07-2021, 10:04 AM)ibreeden Wrote: Hi sarata,
Are you sure it returns 1 for the string '0000'? With me it returns 'None'.
And this is because you only return a value when a letter is encountered that is not '0'. This condition does not occur when you test '0000'.
The solution is to also return count when the for loop finishes.
Or, what I like more: break the loop when a letter is found that is not '0' and only have one return in the function.
def beginning_zeros(x):
    count = 0
    for letters in x:
        if letters == '0':
            count += 1
        else:
            break
    return count

print(beginning_zeros("0001"))
print(beginning_zeros("0000"))
Output:
3 4


Thanks a lot ! Now i get it :)
Reply
#4
(Mar-07-2021, 09:03 AM)saratha Wrote: I wrote this code to count the zeros at the beginning. While i excecuted it for '0001' it gave the right answer(3) but when i ran for '0000' it gives me 1. Can someone explain why? And what should i do for this?

Just for fun!

def beginning_zeros(x):
    count = 0
    for i in range(len(x)):
        if x[i] == '0':
            count += 1
        else:
           pass
    return count

print(beginning_zeros('0.010'))
print(beginning_zeros('1,000,010.05'))
Reply
#5
(Mar-10-2021, 04:49 PM)ebolisa Wrote: Just for fun!
Not so fun as it has range(len(x)) and doesn't solve the task😉
Maybe more fun all your code is .count('0')
>>> s = "1,000,010.05"
>>> s.count('0')
6
Short one that solve the task could be.
>>> s = "0000"
>>> len(s)-len(s.lstrip('0'))
4
>>> s = "001000000"
>>> len(s)-len(s.lstrip('0'))
2
ebolisa likes this post
Reply
#6
(Mar-11-2021, 01:34 AM)snippsat Wrote:
>>> 
>>> len(s)-len(s.lstrip('0'))
2

I understand what you mean. My code was based on the 3rd post (as a rookie to a rookie comment), I didn't read the first 2. So, post 3 is wrong then and the correct answer is
len(s)-len(s.lstrip('0'))
as it's counting 0 to the left of a digit above 0? However, the following code fails.
def beginning_zeros(x):
    count = len(x) - len(x.lstrip('0'))
    # count = 0
    # for i in range(len(x)):
    #     if x[i] == '0':
    #         count += 1
    #     else:
    #         pass
    return count


print(beginning_zeros('0.010'))
print(beginning_zeros('1,000,010.05'))
Reply
#7
(Mar-11-2021, 08:38 AM)ebolisa Wrote: I understand what you mean. My code was based on the 3rd post
No problem is always fine that someone take a look and has other solution 👍

(Mar-11-2021, 08:38 AM)ebolisa Wrote: as it's counting 0 to the left of a digit above 0? However, the following code fails.
Look ok as the task was to count leading 0,so also ,. should break if not specified otherwise.
def beginning_zeros(x):
    count = len(x) - len(x.lstrip('0'))
    return count

print(beginning_zeros("0001"))
print(beginning_zeros("0000"))
print(beginning_zeros('0.010'))
print(beginning_zeros('1,000,010.05'))
Output:
3 4 1 0
def beginning_zeros(x):
    count = 0
    for letters in x:
        if letters == '0':
            count += 1
        else:
            break
    return count

print(beginning_zeros("0001"))
print(beginning_zeros("0000"))
print(beginning_zeros('0.010'))
print(beginning_zeros('1,000,010.05'))
Output:
3 4 1 0
Why i did mention range(len(x)) is this:
Never use "for i in range(len(sequence)):
ebolisa likes this post
Reply
#8
(Mar-11-2021, 10:12 AM)snippsat Wrote: Why i did mention range(len(x)) is this:
Never use "for i in range(len(sequence)):
I'm usually here to learn from people's questions. Thank you for the tip!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020