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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With