函数如果只接受一个输入

jan*_*ane 2 python function

我的代码在这里:http://pastebin.com/bK9SR031.我在Codecademy上做了PygLatin练习并且被带走了,所以大部分都是......初学者.

对不起,这真的很长.问题是,当[Y/N]问题出现时,无论我输入什么,它都表现得好像输入"是".

相关摘录之一:

def TryAgain():
    repeat = raw_input("\nStart over?[Y/N] ").lower()
    if repeat == "y" or "yes" :
        print "OK.\n"
        PygLatin()
    elif repeat == "n" or "no" :
        raw_input("\nPress ENTER to exit the English to Pig Latin Translator.")
        sys.exit()
    else:
        TryAgain()
Run Code Online (Sandbox Code Playgroud)

无论我输入什么,都打印"OK".然后再次启动PygLatin()函数.

Ame*_*ina 6

你的第一个if陈述中的条件:

 if repeat == "y" or "yes":
    print "OK.\n"
    PygLatin()
Run Code Online (Sandbox Code Playgroud)

总是评估True,无论价值如何repeat.这是因为"Yes"它不是一个空字符串(它的布尔值是True),所以or总是会产生True.修复它的一种方法是:

if repeat == "y" or repeat == "yes":
    print "OK.\n"
    PygLatin()
Run Code Online (Sandbox Code Playgroud)

另一个(如下面的sateesh提到)是:

if repeat in ("y","yes"):
    print "OK.\n"
    PygLatin()
Run Code Online (Sandbox Code Playgroud)

您还应该相应地更改else条件