not understood While true:

I have not understood correctly not understood While true:
does it mean that if it return -1 then it is false other wise true

asked 11 Mar '12, 05:16

Owais%20Ahmed's gravatar image

Owais Ahmed
2014721
accept rate: 0%


6 Answers:

What unit? What video? What Python code? What are you asking?

link

answered 11 Mar '12, 05:19

Alberto%20Cid's gravatar image

Alberto Cid
1.0k31030

def get_all_links(page):  
    links = []  
    while True:  
        url, endpos = get_next_target(page)  
        if url:  
              links.append(url)  
              page = page[endpos:]  
        else:  
             break   
    return links
link
This answer is marked "community wiki".

answered 11 Mar '12, 05:21

Owais%20Ahmed's gravatar image

Owais Ahmed
2014721

edited 11 Mar '12, 05:38

Elise's gravatar image

Elise
3.7k51656

While True:

means that you are creating an infinite loop. If you don't create a condition to stop it, it will continue forever.

For example:


n = 3
While True:
if n < 1:
break
print n
n = n - 1

This loop will break as soon as n will be equal to 0.
If the condition to break the loop wasn't there, it would have continued forever.

link

answered 11 Mar '12, 05:25

Elise's gravatar image

Elise
3.7k51656

so for using while true:
one must have to give a condition that at which it will break.
otherwise it will be infinite and continue forever.

so why is this needed when we can do it by giving any other condition ...

link

answered 11 Mar '12, 05:28

Owais%20Ahmed's gravatar image

Owais Ahmed
2014721

If you can give it a condition, no need to use it.
Sometimes, it makes your code more easy, but it does not have to be used if you feel you don't need it.
In the example above, it makes no sense to use it because we can replace True by the very simple condition n > 0.
If we had more complicated / several conditions, it would be very useful.

(11 Mar '12, 05:33) Elise Elise's gravatar image

It is not "needed", you just have it there and you can use it if you want... If you don't want, don't use it.

(11 Mar '12, 05:36) Alberto Cid Alberto%20Cid's gravatar image

The while loop is a control flow construct that checks an expression that must result in a boolean value (True or False). If the expression results in True the while loop will allow the code within it's code block to execute (anything one indentation level greater than the line that has the While loop on it). Example:

while True:
    print 'Hello'
    print 'Goodbye'

The two print statements above will execute FOREVER because the expression that while is using to base its decision on whether to execute the print statements is always true (because it's the boolean value True). To make it so the above print statements don't execute until the end of time there are two options:

  1. Use an expression that will eventually evaluate to False.
  2. Leave the loop manually. Leaving the loop means getting the program to execute code that isn't in the code block that the loop has control of. This is where the break statement and return statement.

I break statement is pretty straight forward....it breaks the program out of the loop. The return statement also changes the execution of code. If you're using a return statement that means you're in a procedure and if your loop is being run that means a different part of the program called that procedure. Executing a return statement returns the program back to the location the procedure was called from (technically just after it) so that is also why a return statement can get your program out of a loop.

The while loop itself doesn't return anything...it just blindly checks "can I let the code within my code block run?" to get the answer it checks to see if the conditional expression associated with it results to True or False.

Hope this helps some.

Guess I spent too much typing :) Looks like others have helped out.

link

answered 11 Mar '12, 05:47

David%20Smith's gravatar image

David Smith
1.2k61236

edited 11 Mar '12, 05:48

Having a same thing eplained in many different ways is a good thing! :)

(11 Mar '12, 06:17) Elise Elise's gravatar image

The while loops works like this:

while condition:

While the condition is true, you execute the code in the while loop, otherwise you don't
while True:

While True is true, you execute the code in the while lopp, otherwise you don't.


As True is always true, it is a never ending loop, or an infinite loop. And the only way to get out of it is to break (which basically means "get out of this loop NOW", or if it's in a procedure a return is possible too. If you choose to return the value, it won't have anything to do with the condition.

example:

def something():
while True:
return False
print something() # will print False

link

answered 11 Mar '12, 06:11

Tom%20Lether's gravatar image

Tom Lether
1.3k1022

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,299
×43
×16

Asked: 11 Mar '12, 05:16

Seen: 296 times

Last updated: 11 Mar '12, 06:17