[Tutor] need a little help

Sean 'Shaleh' Perry shalehperry@attbi.com
Mon, 31 Dec 2001 10:29:45 -0800 (PST)


On 31-Dec-2001 Oliverio Sierra wrote:
> hi, i just downloaded and started studying python a few days ago. it's also 
> the first language i have studied. i am currently writing my first 
> semi-complicated(at least for me) program and i hit a dead end.  my program 
> goes a little like this:
> 
> -there is a menu with 3 choices
> -then a user = input() statement
> -then an if, 2 elif, and an else statements
> 
> i can go into any of the 3 choices, but only once. i need to be able to go 
> into them several times. i have read everything i could find on the net, but 
> had no succes.  i know there is a way for the program to cycle back to where 
> my user = input() statement is.  i apreshiate your help and time. thank you.
> 

this is called a loop.  There are two different kinds of loops in python.

the 'for' loop loops over a sequence of values.

for line in readlines():
    process_input(line)

would call process_input() once for each item in readlines().  The item from
readlines() is stored in the temporary variable 'line'.

the other loop structure is the 'while' loop.  This is just like an if, except
it loops.

end_point = 30

while 1: # loop forever
   val = do_something()
   if val == end_point: break # until we reach an end

this style is common in input situations.

Another is 'while not done:' where done is set to zero initially and then to 1
at some condition in the loop.