Python Forum
leading zero number formatting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
leading zero number formatting
#1
Hi, I want to print out lock combination guesses, all as 4 digits with leading zeros as needed (0000, 0001, 0002, etc.), but I'm having trouble:
#!/usr/bin/env python3
#MoreOnLoops.py

#pad lock guesser
lockCombo = 1000
guess = 0000
while guess != lockCombo:
    guess += 1
    if guess == lockCombo:
        print("After " + str(guess) + " guesses, the correct combo is " + str(guess))
        break
    else:
        print("{:02d}".format(guess))
        #print(str(guess) + " is not the correct combo. Trying next guess.")
        continue
Also, why is 100 not included in the output of the following loop:
for i in range(5, 100, 5):#start at 5, go up to 100 with increments of 5
    print(i)
Output:
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
Reply
#2
>>> guess = 0
>>> '{:0>4d}'.format(guess)
'0000'
with f-strings in 3.6+
>>> guess = 0
>>> f'{guess:0>4d}'
'0000'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-27-2019, 04:14 PM)RedSkeleton007 Wrote: Also, why is 100 not included in the output of the following loop:
because stop in range(start, stop, step) is not included
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jan-27-2019, 04:15 PM)buran Wrote:
>>> guess = 0
>>> '{:0>4d}'.format(guess)
'0000'

Excellent, but the first printed guess, 0000 did not appear, so I moved the guess incrementor to the else:
#pad lock guesser
lockCombo = 1000
guess = 0
#'{:0>4d}'.format(guess)#for python 3
#f'{guess:0>4d}'#for python 3.6+
while guess != lockCombo:
    #guess += 1
    if guess == lockCombo:
        print("After " + str(guess) + " guesses, the correct combo is " + str(guess))
        break
    else:
        print('{:0>4d}'.format(guess))
        #print('{:0>4d}'.format(guess) + " is not the correct combo. " +
        #      "Trying next guess.")
        #print(f'{guess:0>4d}' + " is not the correct combo. " +
        #      "Trying next guess.")
        #print(str(guess) + " is not the correct combo. Trying next guess.")
        guess += 1
        continue
But as a result, the matching 1000 lock combo did not print out:
Output:
0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 <output omitted> 0997 0998 0999
How do I get it to include both the 0000 first guess and the 1000?

(Jan-27-2019, 04:17 PM)buran Wrote: because stop in range(start, stop, step) is not included
Is there a way to get it to print the 100, without having to change stop to 105?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting float number output barryjo 2 912 May-04-2023, 02:04 PM
Last Post: barryjo
  Removing leading whitespaces palladium 1 712 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  comparing zero leading number with a number tester_V 6 2,445 Mar-23-2023, 10:47 AM
Last Post: DeaD_EyE
  Removing leading\trailing spaces azizrasul 8 2,673 Oct-23-2022, 11:06 PM
Last Post: azizrasul
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,748 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  -i option changes sys.path (removes leading empty string '') markanth 6 1,978 Aug-26-2022, 09:27 PM
Last Post: markanth
  How to keep leading zeros with pandas? eeps24 1 6,547 May-20-2020, 07:51 PM
Last Post: deanhystad
  Cancelling 'open directory' leading to crash Fairbz_ 1 2,176 May-08-2020, 03:14 PM
Last Post: DPaul
  Probs with leading zeros falling away :-) Badosc 2 2,861 Dec-04-2018, 08:57 PM
Last Post: Badosc
  leading zeros kerzol81 7 7,048 Apr-23-2017, 03:53 PM
Last Post: kerzol81

Forum Jump:

User Panel Messages

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