0

I want this program to ask the user if they want to continue and if they say yes then it reruns the program and if they say no then it ends the program. I'm stuck and can't figure it out.

name = raw_input("Whats your name?: ")
age = raw_input("How old are you?: ")
if age.isdigit() and int(age) > 15:
    print "Congradulations you can drive!"
elif age.isdigit() and int(age) < 16:
    print "Sorry you can not drive yet :("
else:
    print "Enter a valid number"
print ("it's been very nice getting to know you " + name)
print ("")
Josh C
  • 153
  • 1
  • 2
  • 6

2 Answers2

1

Try putting all the code in a while loop:

While True:
    .... code...
    run_again = raw_input("Run again? ")
    if run_again == 'no':
        break

Another method could be to put the code into a function and call the function again if the user says they want to run again.

Check out this question for more.

Community
  • 1
  • 1
John Howard
  • 61,037
  • 23
  • 50
  • 66
0

I once created a function to do that:

def yorn(question = "[y/n]", strict = True):
    """yorn([question][, strict]) -> user input

    Asks the question specified and if the user input is 'yes' or 'y',
    returns 'yes'.  If the user input is 'no' or 'n', it returns no.  If
    strict is False, many other answers will be acceptable."""
    question = question.strip(" ")
    x = raw_input("%s " % question)
    x = x.lower()
    if (x == "yes") or (x == "y") or ((not strict) and ((x == "yep") or (x == "yeah") or (x == "of course") or (x == "sure") or (x == "definitely") or (x == "certainly") or (x == "uh huh") or (x == "okay") or (x == "ok"))): 
        return True 
    elif (x == "no") or (x == "n") or (not strict and x in ["nope", "uh uh", "no way", "definitely not", "certainly not", "nah", "of course not"]):
        return False
    else:
        return yorn(strict = strict)

To use it, make your program something like this:

again = True
while again:
    name = raw_input("Whats your name?: ")
    age = raw_input("How old are you?: ")
    if age.isdigit() and int(age) > 15:
        print "Congradulations you can drive!"
    elif age.isdigit() and int(age) < 16:
        print "Sorry you can not drive yet :("
    else:
        print "Enter a valid number"
    print ("it's been very nice getting to know you " + name)
    print ("")
    again = yorn("Would you like to try again? ", strict=False)
zondo
  • 19,901
  • 8
  • 44
  • 83