-4
from sys import exit

def answer():
    answer = raw_input("> ")
    if answer == "Yes" or answer == "yes":
        #going to next
        joint()
    elif answer == "No" or answer == "no":
        print "You still have something, I know..."
        again()
    else:
        fubar()

def again():
    again = raw_input("> ")
    if again == "Yes" or again == "yes":
        #going to next
        joint()
    elif again == "No" or again == "no":
        print "You still have something, I know..."
    else:
        fubar()

def fuck():
    print "Fubar'd!"

def joint():
    print "To be continue..."

def question():
    print "Hi duuuude..."
    raw_input("To say 'Hi' press Enter")
    print "Can you help me?"
    answer()

question() 

嗨,你能帮我解决这个问题吗?当我得到答案“否”时,我正在尝试重复“答案”功能。我想“再次”转义功能......还有一种方法可以转义“答案==“是”或答案==“是”:“所以无论我写大写还是小写字母都可以接受答案而不是像菜鸟一样写“是”还是“是”?

4

2 回答 2

3

这通常通过 while 循环来实现。

编辑:正如所指出的,while 循环很好而且清晰,并且避免了递归限制。

从未想过一个简单的答案会产生如此多的选票......

给你举个例子

while True:
    ans = raw_input("Enter only y or n to continue").strip().lower()
    if ans == "y":
        print "Done!"
        break
    elif ans == "n":
        print "No?"
    else:
        print "Not valid input."
于 2012-11-11T17:03:20.420 回答
1

解决您的问题的最简单方法是删除您的again功能,然后递归:

def answer():
    ans = raw_input("> ")
    if ans == "Yes" or ans == "yes":
        #going to next
        joint()
    elif ans == "No" or ans == "no":
        print "You still have something, I know..."
        answer() # again()
    else:
        fubar()

我不得不将你的answer变量重命名为ans,这样它就不会与函数名发生冲突。


对于第二个问题,您想要:

if answer.lower() == "yes":

或者

if answer in ("Yes", "yes"):
于 2012-11-11T17:03:42.183 回答