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
#8
(Nov-09-2018, 10:52 AM)Larz60+ Wrote: Looking at the original code, this is a poor conversion request. In reality all the loop is doing is count number of digits, so to know how to iterate the loop properly, you need to first know the number of digits. Doesn't make sense.
you can get that simply:
num_len = len(str(number))
so, to do as loop:
def display_digits(number):
    counter = 0
    for i in str(number):
        counter += 1
    print(counter)
 
display_digits(26)
display_digits(261234)

# Easy way
print('Easy: for 26: {}'.format(len(str(26))))
print('Easy: for 261234: {}'.format(len(str(261234))))
results:
Output:
2 6 Easy: for 26: 2 Easy: for 261234: 6

I'm not sure if you were saying it was a poor request by me as I was just doing as instructed! Cry
But thank you for your time and effort in helping me with this!
Reply


Messages In This Thread
RE: Help change from a while loop to a for loop - by captwiggum - Nov-09-2018, 10:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop to find the best combination/score KoinKoin 21 29,660 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 1,946 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,742 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 1,783 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,888 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 1,887 Mar-23-2022, 09:47 AM
Last Post: Leyo
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,279 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 1,941 Nov-22-2021, 03:36 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 2,012 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Printing During a Loop apeltes 16 5,458 Oct-21-2021, 12:19 AM
Last Post: apeltes

Forum Jump:

User Panel Messages

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