Python Forum
Help change from a while loop to a for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help change from a while loop to a for loop
#11
(Nov-09-2018, 11:20 AM)stullis Wrote: Larz's explanation is the best. As for my suggestion, here's what I had in mind:

def displayDigits(number):
    for x in range(number):
        divisor = 10 ** x

        if number // divisor == 0:
            return x

    return -1 # In case the number is magically too large

print(displayDigits(26))

Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation How to get program to loop if input date is in the past? xander_thornton45 3 1,785 Oct-11-2024, 10:08 AM
Last Post: Pedroski55
  Loop problem Milczek 1 1,176 Jul-22-2024, 03:34 AM
Last Post: deanhystad
  Loop to find the best combination/score KoinKoin 21 38,156 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 3,579 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 4,765 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 3,288 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 3,487 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 2,877 Mar-23-2022, 09:47 AM
Last Post: Leyo
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 5,804 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 2,775 Nov-22-2021, 03:36 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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