1

I wrote this code. It's like len() function.

def length_itr_for(list):
    total = 0
    for i in list:
        total += 1
    return total

print length_itr_for([1,2,3,4,5,6,7,8])

output is; 8. because in this list, there are 8 value. so len is of this list is 8.

but I don't know how can I write this code with while loop?

while list[i]: etc... I though a few things but I don't know what should I write it here.

edit: actually I tried this code too. but It's not good code. just tried and it didn't work.

def length_itr_whl(list):
    total = 0
    i = 0
    while list[i]:
        total = total + 1
        i = i + 1
    return total

print length_itr_whl([1,2,3,4,5])
bkk
  • 81
  • 1
  • 7
  • 2
    Why do you want to write this with a while loop. It's perfectly fine as a for loop – sshashank124 May 20 '18 at 08:16
  • Of course it's good with for loop. but I don't know. I just want to try with both of them. just for improving. – bkk May 20 '18 at 08:18
  • 2
    Although the intent is good, you won't accomplish anything trying to implement `len` using a `while` loop since you'll get an IndexError for `while list[i]` if you go out of range – sshashank124 May 20 '18 at 08:19
  • 1
    You would instead need to bind your while condition by saying `while i < len(list)` but then you're just using `len` there. It's not the best exercise to practice `while` loops with – sshashank124 May 20 '18 at 08:20
  • okay then, thank you for your interest. – bkk May 20 '18 at 08:21
  • if you do wish to practice basic python concepts, there are lots of online resources to give you practice problems that are specifically designed to help you learn certain things. I would recommend just searching around – sshashank124 May 20 '18 at 08:23
  • If you wanted to reimplement len() from scratch, the most Pythonic way would be to catch the IndexError when the list index is out of bounds (rather than "look before you leap") – eddiewould May 20 '18 at 08:32

4 Answers4

2

You can write a function that tests whether an index is in range for a list:

def validIndex(l, i):
    try:
        _ = l[i]
    except IndexError:
        return False
    return True

I got this code from If list index exists, do X

Then you can use this in your loop:

def length_itr_whl(list):
    total = 0
    index = 0
    while validIndex(list, index):
        total += 1
        index += 1
    return total

You could also use while True: and catch the index error in the loop.

def length_itr_whl(list):
    total = 0
    index = 0
    try:
        while True:
            _ = list[index]
            total += 1
            index += 1
    except IndexError:
        pass
    return total
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
def length(items) :
    idx = 0
    try:
        while True:
            _ = items[idx] 
           idx += 1
    except IndexError:
        return idx
eddiewould
  • 1,555
  • 16
  • 36
0

If you really want to convert this code to a while-loop, you could always do something like this:

def length_itr_whl(list):
    total = 0
    i = 0
    while list[i:i+1]:
        total = total + 1
        i = i + 1
    return total

print length_itr_whl([1,2,3,4,5]) # prints 5
print length_itr_whl([]) # prints 0

This uses the list-slicing mechanism in Python and it does not require any try-block. When the indices are out of range the result will be [] (an empty list), which evaluates to False in Python.

However, why don't you just use the built-in len-function in Python?

Schotmans
  • 86
  • 5
  • vov it is like I wanted. but list[i:i+1] is list[0:1] and how can be happen? I don't understand the event here. by the way, I know what does slicing mean. but here, I didn't get it the event. and actually it is for wondering. Just I want to try for improving my skill. I am newbie in code. – bkk May 20 '18 at 11:07
  • The `while`-loop just needs an expression that can be evaluated to either `True` or `False`. And in Python an empty list (`[]`) evaluates to `False`, causing the while-loop to stop. And you get an empty list when the indices in `list[i:i+1]` are out-of-range. – Schotmans May 20 '18 at 15:26
0

Try this:

list=[1,2,3,4,5]
total = 0
while total != len(list):
    total +=1
J.Z
  • 1
  • 2