<small id='L5VbE'></small><noframes id='L5VbE'>

<i id='L5VbE'><tr id='L5VbE'><dt id='L5VbE'><q id='L5VbE'><span id='L5VbE'><b id='L5VbE'><form id='L5VbE'><ins id='L5VbE'></ins><ul id='L5VbE'></ul><sub id='L5VbE'></sub></form><legend id='L5VbE'></legend><bdo id='L5VbE'><pre id='L5VbE'><center id='L5VbE'></center></pre></bdo></b><th id='L5VbE'></th></span></q></dt></tr></i><div id='L5VbE'><tfoot id='L5VbE'></tfoot><dl id='L5VbE'><fieldset id='L5VbE'></fieldset></dl></div>
    <bdo id='L5VbE'></bdo><ul id='L5VbE'></ul>

<legend id='L5VbE'><style id='L5VbE'><dir id='L5VbE'><q id='L5VbE'></q></dir></style></legend>

      1. <tfoot id='L5VbE'></tfoot>

        我的 python 函数不会返回或更新值

        时间:2023-09-02
        <legend id='OmRgu'><style id='OmRgu'><dir id='OmRgu'><q id='OmRgu'></q></dir></style></legend>

          • <bdo id='OmRgu'></bdo><ul id='OmRgu'></ul>
              <tbody id='OmRgu'></tbody>

            <small id='OmRgu'></small><noframes id='OmRgu'>

            • <i id='OmRgu'><tr id='OmRgu'><dt id='OmRgu'><q id='OmRgu'><span id='OmRgu'><b id='OmRgu'><form id='OmRgu'><ins id='OmRgu'></ins><ul id='OmRgu'></ul><sub id='OmRgu'></sub></form><legend id='OmRgu'></legend><bdo id='OmRgu'><pre id='OmRgu'><center id='OmRgu'></center></pre></bdo></b><th id='OmRgu'></th></span></q></dt></tr></i><div id='OmRgu'><tfoot id='OmRgu'></tfoot><dl id='OmRgu'><fieldset id='OmRgu'></fieldset></dl></div>
            • <tfoot id='OmRgu'></tfoot>

                1. 本文介绍了我的 python 函数不会返回或更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  def getMove(win,playerX,playerY):
                  
                      #Define variables.
                      movePos = 75
                      moveNeg = -75
                      running = 1
                  
                      #Run while loop constantly to update mouse's coordinates.
                      while(running):
                  
                          mouseCoord = win.getMouse()
                          mouseX = mouseCoord.getX()
                          mouseY = mouseCoord.getY()
                          print "Mouse X = ", mouseX
                          print "Mouse Y = ", mouseY
                  
                          if mouseX >= playerX:
                              playerX = movePos + playerX
                              running = 0
                          elif mouseX <= playerX:
                              playerX = moveNeg + playerX           
                              running = 0
                          elif mouseY >= playerY:
                              playerY = movePos + playerY            
                              running = 0
                          elif mouseY <= playerY:
                              playerY = moveNeg + playerY            
                              running = 0
                      return playerX,playerY
                  
                  def main():
                  
                      #Create game window.
                      win = GraphWin("Python Game", 500, 500)
                      drawBoard(win)
                  
                      #Define variables.
                      playerX = 75
                      playerY = 125
                      keyX = 325
                      keyY = 375
                      running = 1
                  
                  
                      #Create Key and Player objects, draw the key, but don't draw the player yet.
                      key = Text(Point(keyX,keyY),"KEY")
                      key.draw(win)
                  
                      while(running):
                          print "player X = ", playerX
                          print "Player Y = ", playerY
                          drawBoard(win)
                          getMove(win,playerX,playerY)
                          player = Circle(Point(playerX,playerY),22)
                          player.setFill('yellow')
                          player.draw(win)
                  main()
                  

                  我正在使用图形库来创建游戏.我的播放器和钥匙被绘制在正确的位置.但是,当调用 getMove 函数时,我的 playerX 和 playerY 没有更新.我添加了调试打印语句以在运行游戏时找到它们的值,它始终是 75 和 125.求助!

                  I am using a graphics library to create a game. My player and key are drawn in the correct places. However, when calling the getMove function, my playerX and playerY do not update. I have added debug print statements to find their values while running the game and it is always 75 and 125. Help!

                  推荐答案

                  在python中,整数是不可变的——当你给一个变量分配一个新的整数值时,你只是让变量指向一个新的整数,而不是改变什么它指向的旧整数的值是.

                  In python, integers are immutable - when you assign a new integer value to a variable, you are just making the variable point to a new integer, not changing what the old integer it pointed to's value was.

                  (python 中可变对象的一个示例是列表,您可以对其进行修改,并且指向该列表的所有变量都会注意到更改 - 因为 LIST 已更改.)

                  (An example of a mutable object in python is the list, which you can modify and all variables pointing to that list will notice the change - since the LIST has changed.)

                  同样,当您将变量传递给 python 中的方法,然后更改该变量在该方法中指向的内容时,您不会更改该变量指向该方法之外的内容,因为它是一个新变量.

                  Similarly, when you pass a variable into a method in python and then alter what the variable points to in that method, you do not alter what the variable points to outside of that method because it is a new variable.

                  为了解决这个问题,将返回的 playerX,playerY 分配给方法外的变量:

                  To fix this, assigned the returned playerX,playerY to your variables outside the method:

                  playerX, playerY = getMove(win,playerX,playerY)

                  这篇关于我的 python 函数不会返回或更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:这个“和"是什么?声明中究竟在做什么? 下一篇:从函数列表中返回项目.Python

                  相关文章

                  <tfoot id='zWPpU'></tfoot>
                2. <small id='zWPpU'></small><noframes id='zWPpU'>

                3. <legend id='zWPpU'><style id='zWPpU'><dir id='zWPpU'><q id='zWPpU'></q></dir></style></legend>
                  <i id='zWPpU'><tr id='zWPpU'><dt id='zWPpU'><q id='zWPpU'><span id='zWPpU'><b id='zWPpU'><form id='zWPpU'><ins id='zWPpU'></ins><ul id='zWPpU'></ul><sub id='zWPpU'></sub></form><legend id='zWPpU'></legend><bdo id='zWPpU'><pre id='zWPpU'><center id='zWPpU'></center></pre></bdo></b><th id='zWPpU'></th></span></q></dt></tr></i><div id='zWPpU'><tfoot id='zWPpU'></tfoot><dl id='zWPpU'><fieldset id='zWPpU'></fieldset></dl></div>

                    • <bdo id='zWPpU'></bdo><ul id='zWPpU'></ul>