user1692517
user1692517

Reputation: 1152

Looping a raw_input if an invalid response is given

I'm doing a simple game that asks if users want to continue by inputting Y/N. But if a user inputs something other than that, I would like the question to loop. How would I do this?

def start():
    print "Hello there.";   time.sleep(.5)
    myname = raw_input("What is your name? ");  time.sleep(.5)
    print "Welcome %s, this is..." %myname; time.sleep(.5)
    uname = myname.upper()
    print "\t\t\tTHE ADVENTURES OF %s" %uname
    choice0 = raw_input("\nWould you like to play the game? Y/N ")
    if choice0 == "Y":
            gameon
    if choice0 == "N":
            print "Alright, bye!"
    else:   
            print "Invalid input."

Upvotes: 0

Views: 345

Answers (1)

inspectorG4dget
inspectorG4dget

Reputation: 114035

choice0 = ''
allowed = ["y", "n"]
while choice0.lower() not in allowed:
    choice0 = raw_input("\nWould you like to play the game? Y/N ")

Upvotes: 2

Related Questions