无法在更改变量时分配函数调用。

huangapple go评论151阅读模式
英文:

Can't assign function call while changng a variable

问题

以下是已翻译的内容:

我正在检查一个我需要提交的项目(这是一款战舰游戏),但由于某种原因,当它运行到下面这部分时,会显示“无法分配函数调用”的错误,而实际上这只是上面的一部分的复制粘贴(稍作修改),并且没有出现错误。您看到了错误吗?

elif y == "v":
    if a + 3 > 4:
        return "将船放得更高一些,否则它将离开游戏板"
    else:
        board(a)(b) = "V"
        a = a + 1
        board(a)(b) = "V"
        a = a + 1
        board(a)(b) = "V"
        return board
英文:

I was checking a project that i have to turn in (it's a battleship game) and for some reason when it runs "through" the section bellow it says "can't assign function call" when it's a copy paste of a piece of just above (with a couple changes) and it gives no error. Do you see the error?

    '''
     elif y == "v":
            if a + 3 > 4:
                return "put the boat higher, here it leaves the board"
            else:
                board(a)(b) = "V"
                a = a + 1
                board(a)(b) = "V"
                a = a + 1
                board(a)(b) = "V"
                return board        

    '''

答案1

得分: 1

首先,我强烈建议您使用Python 3,阅读此文档

我不知道什么是board,所以我将回答两种情况。

  1. board不是一个函数,而是嵌套的Python列表。
    在这种情况下,只需将()更改为[]以访问数组。

  2. board是一个函数。
    在这种情况下,你肯定是错的。board()是一个函数调用,将返回函数的结果。因此,你不能将“V”分配给你的函数调用。这是非常自然的。

现在,检查一下您的情况,愿您编程愉快。

英文:

First of all, I highly recommend you to use python 3, read this.

And I don't know what is board, so I will answer for two cases.

  1. board is not a function, nested python list
    In this case, just change () to [] to access array.
  2. board is a function
    In this case, you're definitely wrong. board() is a function call and will return function result. So, you cannot assign "V" into your function call. This is pretty natural.

Now, check out what is your case and happy coding.

答案2

得分: 0

尝试将board(a)(b)替换为board[a][b],而不是使用[]运算符进行矩阵访问,可能是你正在使用()进行调用。但如果没有更多信息,很难确定。

英文:

Maybe instead of accessing a matrix with the [] operator you are making calls with (). So try replacing board(a)(b) with board[a][b] but without more information is really hard to tell.

huangapple
  • 本文由 发表于 2020年1月7日 02:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定