Python Forum
leading zero number formatting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
leading zero number formatting
#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


Messages In This Thread
leading zero number formatting - by RedSkeleton007 - Jan-27-2019, 04:14 PM
RE: leading zero number formatting - by buran - Jan-27-2019, 04:15 PM
RE: leading zero number formatting - by buran - Jan-27-2019, 04:17 PM
RE: leading zero number formatting - by RedSkeleton007 - Jan-27-2019, 04:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting float number output barryjo 2 937 May-04-2023, 02:04 PM
Last Post: barryjo
  Removing leading whitespaces palladium 1 738 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  comparing zero leading number with a number tester_V 6 2,516 Mar-23-2023, 10:47 AM
Last Post: DeaD_EyE
  Removing leading\trailing spaces azizrasul 8 2,759 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,781 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  -i option changes sys.path (removes leading empty string '') markanth 6 2,020 Aug-26-2022, 09:27 PM
Last Post: markanth
  How to keep leading zeros with pandas? eeps24 1 6,595 May-20-2020, 07:51 PM
Last Post: deanhystad
  Cancelling 'open directory' leading to crash Fairbz_ 1 2,202 May-08-2020, 03:14 PM
Last Post: DPaul
  Probs with leading zeros falling away :-) Badosc 2 2,895 Dec-04-2018, 08:57 PM
Last Post: Badosc
  leading zeros kerzol81 7 7,133 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