Help change from a while loop to a for loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Help change from a while loop to a for loop (/thread-13962.html) Pages:
1
2
|
Help change from a while loop to a for loop - captwiggum - Nov-08-2018 Hi, I'm a total beginner and for class I have to change this while loop to a for loop and I just can't figure it out, help would be much appreciated. It is a simple function to count the number of digits in a number. This is what I have. Thanks def displayDigits(number): counter = 0 while(number > 0): number = number // 10 counter = counter + 1 print(counter) displayDigits(26) RE: Help change from a while loop to a for loop - Larz60+ - Nov-08-2018 suggest a reading assignment: https://wiki.python.org/moin/ForLoop RE: Help change from a while loop to a for loop - captwiggum - Nov-08-2018 (Nov-08-2018, 10:17 PM)Larz60+ Wrote: suggest a reading assignment: https://wiki.python.org/moin/ForLoop Hi, Thanks for taking the time to reply and suggest a link but I've done all the reading on this, I've taken it apart, spent hours at it, I know it's simple but I've just hit a brick wall. Thanks RE: Help change from a while loop to a for loop - Larz60+ - Nov-09-2018 show what you have tried here'a a simple for loop that starts at 2 increments by 6 up to 38 Since it is zero based, terminator is one more (39) >>> for x in range(2, 39, 6): ... print(x) ... 2 8 14 20 26 32 38 >>> RE: Help change from a while loop to a for loop - stullis - Nov-09-2018 An obvious implementation of a for loop would limit the number of digits you could count. To prevent that, your iterable should be based upon the number passed in. That would ensure that you have enough iterations for effectively cover the number of digits of any number you could provide. Instead of repeatedly doing floor division, you could use the values in a range as exponents. This way, each iteration increases the divisor by a magnitude of ten and you simultaneously have your "count" for the output: for x in range(20): divisor = 10 ** x print(f"For {x}, the divisor is {divisor}.")Now, how could you use that idea in your function? On a side note, a function should always have a return. The print() call should be replaced by a return and then you call the function like this: print(displayDigits(26)) RE: Help change from a while loop to a for loop - captwiggum - Nov-09-2018 (Nov-09-2018, 12:33 AM)Larz60+ Wrote: show what you have tried Hi, All I can achieve are various versions of this. Thanks again def displayDigits(number): for i in range(number): number = number // 10 print(number) displayDigits(26) (Nov-09-2018, 02:44 AM)stullis Wrote: An obvious implementation of a for loop would limit the number of digits you could count. To prevent that, your iterable should be based upon the number passed in. That would ensure that you have enough iterations for effectively cover the number of digits of any number you could provide. Thanks for your advice but I honestly don't know how to use what you have shown me. I am totally stuck here. RE: Help change from a while loop to a for loop - Larz60+ - Nov-09-2018 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:
RE: Help change from a while loop to a for loop - captwiggum - Nov-09-2018 (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. I'm not sure if you were saying it was a poor request by me as I was just doing as instructed! But thank you for your time and effort in helping me with this! RE: Help change from a while loop to a for loop - Larz60+ - Nov-09-2018 no, the assignment has a poor example for conversion to a loop RE: Help change from a while loop to a for loop - stullis - Nov-09-2018 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)) |