Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这个模块有什么问题?_Python - Fatal编程技术网

Python 这个模块有什么问题?

Python 这个模块有什么问题?,python,Python,我已经三次检查了这个python模块语句的语法,但它不起作用 correct = 0 ahtriggercount = 0 def IsCorrect(): #Increase correct count. correct = correct + 1 #Lower autohelp trigger count. if ahtriggercount > 0: ahtriggercount = ahtriggercount - 1 一般的“语

我已经三次检查了这个python模块语句的语法,但它不起作用

correct = 0
ahtriggercount = 0

def IsCorrect():
    #Increase correct count.
    correct = correct + 1
    #Lower autohelp trigger count.
    if ahtriggercount > 0:
        ahtriggercount = ahtriggercount - 1
一般的“语法错误”(IDE没有提供进一步的信息)被抛出到

if ahtriggercount > 0:

怎么了?为什么我们不能访问ahtriggercount变量?

您需要声明ahtriggercount并更正为全局变量,因为您正在修改它们的值

def IsCorrect():
    global ahtriggercount, correct
    #Increase correct count.
    correct = correct + 1
    #Lower autohelp trigger count.
    if ahtriggercount > 0:
        ahtriggercount = ahtriggercount - 1

您需要告诉python您正在访问一个全局变量。像这样:

correct = 0
ahtriggercount = 0

def IsCorrect():
    global correct, ahtriggercount
    #Increase correct count.
    correct = correct + 1
    #Lower autohelp trigger count.
    if ahtriggercount > 0:
        ahtriggercount = ahtriggercount - 1
默认情况下,全局变量是只读的,除非您使用Global声明


您还存在空白问题。其中一行上有一些奇怪的空格。当我删除空格并再次添加它们时,我不再得到IndentationError。

在不知道错误消息的情况下,我们只能猜测。然而,您的代码片段在语法上似乎是正确的。也许你混淆了空格和制表符,或者在一行中放弃了空格或制表符?正确的缩进在Python中至关重要。

错误是什么?如果需要帮助,请始终包含错误。更好的办法是阅读错误,它可能会告诉你问题出在哪里,直到没有骰子为止。我们完全按照描述将变量声明为全局变量,但仍然抛出相同的语法错误。它只是说“无效语法”,没有进一步的数据。甚至可以在独立模块的顶部声明变量吗?您有一些不正确的空格。我已经在上面改正了。如果您将其复制并粘贴到解释器中,它现在就可以正常工作。重新设计了我的代码,并根据需要使用了global语句。泰。学习一门新语言总是一种冒险。