7

Why can't while loop be used on a range function in python ?

The code:

def main():
  x=1;

  while x in range(1,11):
     print (str(x)+" cm");


if __name__=="__main__":
    main();

executes as an infinite loop repeatedly printing 1 cm

4
  • 2
    The value of x never changes, so it continuously meets your "while" criterion. Commented Apr 5, 2018 at 17:15
  • Can you write your code in a code box, with proper indentation and whatnot? Look in the help section to find out how to make a code box (4 spaces or CTRL+K) Commented Apr 5, 2018 at 17:15
  • 1
    It can. You never change the value of x so it's always in the range. Commented Apr 5, 2018 at 17:16
  • 1
    What does "use a while loop on a range" even mean? If it means "iterate over the range", then the answer is "because that's what for loops are for". Commented Apr 5, 2018 at 17:16

4 Answers 4

9

Simply we can use while and range() function in python.

>>> while i in range(1,11):
...     print("Hello world", i)
... 
Hello world 1
Hello world 2
Hello world 3
Hello world 4
Hello world 5
Hello world 6
Hello world 7
Hello world 8
Hello world 9
Hello world 10

>>> i
10
Sign up to request clarification or add additional context in comments.

2 Comments

It is giving following error - NameError: name 'i' is not defined. Did you mean: 'id'?.
@Pratik Sharma, use for loop, not use while.
7

For what you're doing, a for loop might be more appropriate:

for x in range(1,11):
    print (str(x)+" cm")

If you want to use while, you need to update x since you'll otherwise end up getting the infinite loop you're describing (x always is =1 if you don't change it, so the condition will always be true ;)).

3 Comments

I imagine it's not the best performance, right? Because it creates a tuple, which isn't needed, right?
@DennyWeinberg range(1,11) isn't a tuple but arguments for the range() function.
I thought range created a tuple (returns a tuple), but thats not right. Sorry
1

while just evaluates boolean (true or false).

def main():
  x=1;

  while x in range(1,11):
     print(str(x)+" cm");


if __name__=="__main__":
    main();

In the above code x is initialised to 1 and it is in the range(1,11) so the while executed and went in loop since the condition in while x in range(1,11): is satisfied every time. To fix this, increment x in the while loop ex:

while x in range(1,11):
    print (str(x)+" cm")
    x= x+1

Comments

0

Outside of a for-loop, x in range(..) mean something different. It's a test that returns a boolean

print(1 in range(1,11)) # prints True

So your while-loop actually means this:

def main():
  x=1;

while True if x in range(1,11) else False:
     print (str(x)+" cm");


if __name__=="__main__":
    main();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.