【问题标题】:Syntax error at second definition in PythonPython中第二个定义的语法错误
【发布时间】:2016-06-23 12:40:49
【问题描述】:
def specificChecker(someThing, checker):
    if checker == None:
        return someThing
    elif checker == True:
        return not someThing
    else:
        return None

def whatDoesTheCheckerSay(someThing):
    if specificChecker(someThing) == someThing:
        return 'The checker value was False on that one.'
    elif specificChecker(someThing) == not someThing:
        return 'The checker value was True on that one.'
    elif specificChecker(someThing) == None:
        return 'Something irregular happend. The checker value wasn\'t None or True.'
    else:
        return 'Something went really wrong. This doesn\'t even not work.'

reallySomeThing = input('Type in really some thing: ')
theChecker = input('Set the checker to something: ')

print(specificChecker(reallySomeThing, theChecker))
print(whatDoesTheCheckerSay(reallySomeThing)) # This can be made more efficient, right?

def one(someShit):
    return someShit + ' AWWW YEAH!'

def two(someShit):
    return one(someShit)

print(two(input('Type in some kind of stuff: ')))

我是一个自学成才的初学者,所以这肯定是一些笨拙的基础知识。我正在使用 IDLE shell,并且在我的代码的第二个定义语句中反复出现语法错误。请帮忙?

【问题讨论】:

  • 它是== not something,应该是!= somethingis not something
  • @nbryans:提is 并说“类似于==”真是个坏主意。 is 表示身份测试; x is y 仅当 xy 是对完全相同的对象的引用。一般来说,你只使用 is 和语言指定的单例(NoneNotImplementedEllipsis,但不是 True/False,即使它们是单例,PEP8 建议使用隐式真值测试。原因)、类型(如果您想要特定类的实例,而不是其子类,type(obj) is MyClass)和哨兵(sentinel = object(); val = mydict.get(key, sentinel); if val is not sentinel:)。

标签: python function syntax definition


【解决方案1】:

你不能使用这条线:

elif specificChecker(someThing) == not someThing:

这个必须写

elif specificChecker(someThing) != someThing:

成为有效的 Python。

这也是有效的,但可能不太可读:

elif (specificChecker(someThing)) == (not someThing):

OP 编辑​​后:

新错误是参数(始终为 1)与需要 2 个参数的函数不匹配。您必须将两个参数传递给specificChecker 而不是一个

【讨论】:

【解决方案2】:

第 12 行:elif specificChecker(someThing) == not someThing:

如果您想检查某个变量是否不是某个变量,请使用is not 表示布尔值或使用!= 表示值和字符串

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2013-12-29
    相关资源
    最近更新 更多