# While loop Problem

**URL:** <https://discuss.python.org/t/while-loop-problem/56099>\
**Category:** Python Help\
**Tags:** help\
**Created:** [June 18, 2024, 8:12pm UTC](https://discuss.python.org/t/while-loop-problem/56099 "2024-06-18T20:12:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lubna](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@Lubna](https://discuss.python.org/u/Lubna)\
**Post date:** [June 18, 2024, 8:12pm UTC](https://discuss.python.org/t/while-loop-problem/56099/1 "2024-06-18T20:12:04Z")

</div>

i=int(input(“Enter your age:”))  
while (i\<18) :  
i=int(input(“Enter your height:”))  
if (i\<5):  
print(“You are not eligible for the ride”)  
else:  
(“You are eligible for the ride”)

Hey I am new learning python. I was practising while loop so came across random code to practice. Can anyone tell how its output will be because The output I am looking for is not in real so can anyone explain what does the code exactly means

---

<div class="post-metadata">

**Author:** ![moosemaimer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/moosemaimer/32/15091_2.png) [@moosemaimer](https://discuss.python.org/u/moosemaimer)\
**Post date:** [June 18, 2024, 8:57pm UTC](https://discuss.python.org/t/while-loop-problem/56099/2 "2024-06-18T20:57:39Z")

</div>

A `while` loop means “repeat the following block of code until the condition is not met.” The loop is set to run `while (i < 18)`, but then `i` is overwritten by `i=int(input(“Enter your height:”))`. So any value for height less than 18 will cause the loop to stop once it finishes the current iteration. We can’t see how much of the code is inside that block (use the \</\> button to add formatting to your posts) but I can’t imagine that’s the intended result.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [June 19, 2024, 12:34am UTC](https://discuss.python.org/t/while-loop-problem/56099/3 "2024-06-19T00:34:03Z")

</div>

> A `while` loop means “repeat the following block of code until the  
> condition is not met.” The loop is set to run `while (i < 18)`, but  
> then `i` is overwritten by `i=int(input(“Enter your height:”))`. So any  
> value for height less than 18 will cause the loop to stop once it  
> finishes the current iteration.

To my eye the loop logic is a bit odd, because it at least starts  
examining the age (in the `i` variable) and then on subsequent loops  
examines the height (because that is what is in the `i` variable then).

Lubna, you will do better to give these distinct vairable names,  
example:

```
 age =int(input("Enter your age:"))
 while (age <18) :
   height=int(input("Enter your height:"))
 if (height<5):

```

and so forth.

Also, it looks like this should be an `if` instead of a `while`. The age  
isn’t going to change, so either the `age>=18`, in which case the while  
is nver entered (because `age<18` is false) \_or `age<18`, in which case  
the loop runs forever, because the age won['t get changed.

> We can’t see how much of the code is inside that block (use the \</\>  
> button to add formatting to your posts) but I can’t imagine that’s the  
> intended result.

I’m on email, so I get it direct 🙂 Here it is:

```
 i=int(input("Enter your age:"))
 while (i<18) :
   i=int(input("Enter your height:"))
 if (i<5):
   print("You are not eligible for the ride")
 else:
   ("You are eligible for the ride")

```

Also, indentation is critical in Python. There’s no `end-while` or  
`end-if` or curly brackets to mark out where in the logic some code  
belongs. It is all indentation. So in the above, this line:

```
 i=int(input("Enter your height:"))

```

is in the while-loop, but nothing else is because it is not indented  
underneath the while.

Also, the `else:` part of your if statement doesn’t do anything. This  
line:

```
 ("You are eligible for the ride")

```

is legal Python, but it just gets evaluated. And then discarded. To see  
it printed you need to call print, i.e.:

```
 print("You are eligible for the ride")

```
