Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with code
#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


Messages In This Thread
problem with code - by saratha - Mar-07-2021, 09:03 AM
RE: problem with code - by ibreeden - Mar-07-2021, 10:04 AM
RE: problem with code - by saratha - Mar-07-2021, 10:08 AM
RE: problem with code - by ebolisa - Mar-10-2021, 04:49 PM
RE: problem with code - by snippsat - Mar-11-2021, 01:34 AM
RE: problem with code - by ebolisa - Mar-11-2021, 08:38 AM
RE: problem with code - by snippsat - Mar-11-2021, 10:12 AM
RE: problem with code - by ebolisa - Mar-11-2021, 01:32 PM

Forum Jump:

User Panel Messages

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