Home > Software engineering >  Trying to use while loop for range. The range is not printing
Trying to use while loop for range. The range is not printing

Time:11-28

I'm trying to make a program that asks for two inputs between 0 and 100. Then I need to make a while loop to print the range of the inputs and the sum. My while loop does nothing and I'm not sure what to do?

while i in range(l,o):
    print(i)

CodePudding user response:

You need use for loop.

for i in range(l,o):
    print(i)

CodePudding user response:

while is used to run code while a boolean is true.
You can use for to iterate over a range of numbers.

for i in range(l,o):
    print(i)

CodePudding user response:

You using while instead of for

for i in range(l,o):
    print(i)

CodePudding user response:

By using while instead of for, the code i in range(1,o) becomes a boolean. So the condition is if i is in range(1,o). The loop continues if True.

CodePudding user response:

Assuming it truly has to be a while loop, you can do it like this:

while l < o:
    print(l)
    l  = 1

The reason your implementation doesn't work is that while is followed by a boolean statement. So i in range(l, o) is evaluated and will either return True (in which case your while loops runs forever), False (in which case our loop never starts), or if i is undefined throws an NameError exception.

  • Related