Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python while loop range function

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

like image 357
Anupama Avatar asked Apr 05 '18 17:04

Anupama


1 Answers

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
like image 198
Viraj Wadate Avatar answered Oct 15 '22 21:10

Viraj Wadate