如果给出无效响应,则循环raw_input

时间:2012-09-26 02:05:48

标签: python loops

我做了一个简单的游戏,询问用户是否想通过输入Y / N来继续。 但是如果用户输入的内容不是那样的话,我想问这个问题。我该怎么做?

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."

1 个答案:

答案 0 :(得分:2)

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