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


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

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