我有这个功能,应该计算点,但它不会添加它们

时间:2016-04-19 18:29:17

标签: python

我有这个函数应该计算点数,但它不会添加它们:

import sys
from PyQt4 import QtGui, QtCore
import resources

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(0, 0, 1280, 800)
        self.setWindowTitle("E.S Quiz")
        self.home()


    def home(self):
        pic = QtGui.QLabel(self)
        pic.setGeometry(0, 0, 1280, 800)
        pic.setPixmap(QtGui.QPixmap(":/images/background.png"))
        btn = QtGui.QPushButton("", self)
        btn.resize(150, 120)
        btn.move(600, 400)
        btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        btn.setObjectName('btn')
        btn.setStyleSheet("#btn {background-image: url(':/images/Button1.png'); border: none; }"
        "#btn:hover { background-image: url(':/images/Button1Hover.png'); }"
        "#btn:pressed { background-image: url(':/images/Button1Press.png'); }")
        btn.clicked.connect(self.test)
        self.show()

    def test(self):
        print "Here"




def startup():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

startup()

以下是我在其中使用它的示例:

def Correct(totalPoints):
    print "Correct"
    totalPoints = totalPoints+1
    print "You have", totalPoints,"points"

7 个答案:

答案 0 :(得分:1)

我认为你混合使用本地和全局命名空间。如果您将Correct(totalPoints)更改为此:

def Correct(totalPoints):
    return totalPoints + 1

并在您的"主要代码":

print Correct(totalPoints)

totalPoints = Correct(totalPoints)
print "You have", totalPoints, "points"

它应该有用。

希望这有帮助!

答案 1 :(得分:0)

mysql_prep正在您的函数中克隆。要做你想做的事,你应该根据功能结果设置totalPoints,如下所示:

totalPoints

这忽略了为什么你首先想要这样一个函数,但它应该有效。

答案 2 :(得分:0)

您需要Correct函数才能返回某些内容。现在,您正在打印结果并更改totalPoints局部变量(当作为参数传递给函数时复制了该变量)。当您在totalPoints函数内部对Correct进行更改时,它只会更改本地的复制变量,而不是传入的变量。

请改为尝试:

def Correct(totalPoints):
    print "Correct"
    totalPoints = totalPoints+1
    print "You have", totalPoints,"points"
    return totalPoints

现在它返回了totalPoints,该变量可以恢复到常规程序。

答案 3 :(得分:0)

1)您没有从函数返回值 2)全局变量最好在函数之外改变    - 在函数外部,将变量设置为函数返回的任何值或    - 如果你必须在函数内部进行,你想在函数的开头声明它是全局的。

def Correct(totalpoints):
    return totalpoints += 1

...

if NBAANSWER == NBA2[NBAQues]:
    print "Correct!"
    totalpoints = Correct(totalPoints)
    print "You have ", totalpoints, "points"

答案 4 :(得分:0)

totalPoint必须声明为全局变量

并在函数中进行引用,以便python可以更新该值而不创建新变量

def Correct(totalPoints):
    global totalPoints
    print "Correct"
    totalPoints = totalPoints+1
    print "You have", totalPoints,"points"

答案 5 :(得分:0)

totalPoints按值传递。这意味着创建了一个新副本,并在Correct函数中操作该副本。由于修改后的totalPoints永远不会返回,因此会丢失。你需要这样的东西:

def Correct(totalPoints):
    return totalPoints += 1

并用

调用它
totalPoints = Correct(totalPoints)

代码不显示原始的totalPoint定义,但这应该让您朝着正确的方向前进。

答案 6 :(得分:0)

您没有返回功能输出。它应该是

def Correct(mytotalPoints):
    print "Correct"
    mytotalPoints += 1
    print "You have", mytotalPoints,"points"
    return mytotalPoints

您现在应该将正确的值返回给main函数。您还应该将缩进修复为正确。您需要显示定义totalPoints的位置。

    elif answer == 'nba':
        NBAQues = random.randint(0,len(NBA1)-1)
    # Is this the way you meant it to be indented?
    NBAVar = NBA1[NBAQues]

    print NBAVar
    NBAAnswer = raw_input()
    NBAAnswer = NBAAnswer.lower()
    if NBAAnswer == NBA2[NBAQues]:
        # Make sure totalPoints was initialized properly
        totalPoints = Correct(totalPoints)
        print totalPoints
    elif NBAAnswer != NBA2[NBAQues]:
        print "False. The correct answer was", NBA2[NBAQues]
        NBA1.remove(NBAVar)
        NBA2.remove(NBA2[NBAQues])
        print "Choose another."