tags:

views:

101

answers:

4

Once the program prints, it shuts down. How do I make it returns to the top of the code so that it loops, indefinitely asking for the users name?

code:

from time import sleep

name = raw_input ("Please enter your name: ")

print "Hello", name, "- good to see you!"
sleep(2.00)

pseudo-code:

from time import sleep

A
name = raw_input ("Please enter your name: ")

print "Hello", name, "- good to see you!"
sleep(2.00)
return to A
+4  A: 
while True:
    # do something
    # do something else
    # do more things

For your specific example:

from time import sleep

while True:
    name = raw_input ("Please enter your name: ")

    print "Hello", name, "- good to see you!"
    sleep(2.00)

The general format of this loop is as follows:

while <condition>:
    <code>

Each time the loop runs, it checks to see if <condition> is a true value (True obviously is, but you can also have more complex conditions like foo < 3 or the like). If it is, then it runs <code>, then repeats. If it isn't, then it finishes looping and moves on in the rest of the program.

There's more information on looping in the Python documentation.

Amber
+6  A: 

You should read some basic documentation, like this: http://docs.python.org/tutorial/datastructures.html#looping-techniques

antrix
A: 
from time import sleep

while True:
    name = raw_input ("Please enter your name: ")

    print "Hello", name, "- good to see you!"
    sleep(2.00)
acqu13sce
seems I was a little bit slow
acqu13sce
A: 

two methods can done. 1. use "do-while" 2. use "while(true) and if to break"

Tim Li
Python doesn't have a do-while
Falmarri