简体   繁体   English

我一直收到此 Python 代码的语法错误消息

[英]I Keep Having a Syntax Error Message With This Python Code

Having issue with small python script.小型 python 脚本存在问题。 trying to add 1 to a global variable every 3 iterations.尝试每 3 次迭代将 1 添加到全局变量。 I keep seeing我不断看到

"for 3 in scalerVal: ^ SyntaxError: cannot assign to literal" “对于 scalerVal 中的 3:^ SyntaxError:无法分配给文字”

I will appreciate an answer我将不胜感激

    x1 = 0
    x2 = 0
    x3 = 0
    x4 = 0
    x5 = 0
    x6 = 0
    x7 = 0
    x8 = 0
    x9 = 0

    itVal = 0


    scalerVal = 3

    # -- STEP 1: --
    # (greatest value) = 3 * itVal + itVal
    # adder = (greatest value) - (current value) = (differnce in value) + itVal
    # scaler = itVal - intVal - itVal

    # -- STEP 2: --
    # add the adder to all n values

    def a1():

      global x1
      x1 = x1 + 3
      global x2
      x2 = x2 + 2
      global x3
      x3 = x3 + 1
      global x4
      x4 = x4 + 2
      global x5
      x5 = x5 + 2
      global x6
      x6 = x6 + 1
      global x7
      x7 = x7 + 1
      global x8
      x8 = x8 + 1
      global x9
      x9 = x9 + 1

      global scalerVal
      for 3 in scalerVal:
        scalerVal + 1
        return()

      global itVal
      if itVal == 0:
        #gVal = 3 * itVal + itVal
        #adder = gVal - x1 + itVal
        #x1 = x1 + adder
        itVal = itVal + 1
        print(x1, x2, x3, x4, x5, x6, x7, x8, x9, itVal)
        return()

      else:
        gVal = scalerVal * itVal + itVal
        adder = gVal - x1 + itVal
        x1 = x1 + adder
        itVal = itVal + 1
        print(x1 , x2, x3, x4, x5, x6, x7, x8, x9, itVal)
        return()

      return()

    def a2():

      global x1
      x1 = x1 + 2
      global x2
      x2 = x2 + 3
      global x3
      x3 = x3 + 2
      global x4
      x4 = x4 + 2
      global x5
      x5 = x5 + 2
      global x6
      x6 = x6 + 2
      global x7
      x7 = x7 + 1
      global x8
      x8 = x8 + 1
      global x9
      x9 = x9 + 1

      global itVal

      return()

    def a3():

      global x1
      x1 = x1 + 1
      global x2
      x2 = x2 + 2
      global x3
      x3 = x3 + 3
      global x4
      x4 = x4 + 1
      global x5
      x5 = x5 + 2
      global x6
      x6 = x6 + 2
      global x7
      x7 = x7 + 1
      global x8
      x8 = x8 + 1
      global x9
      x9 = x9 + 1

      global itVal
      if itVal == 0:
        #gVal = 3 * itVal + itVal
        #adder = gVal - x3 + itVal
        #x3 = x3 + adder
        itVal = itVal + 1
        print(x1, x2, x3, x4, x5, x6, x7, x8, x9, itVal)
        return()

      else:
        gVal = 3 * itVal + itVal
        adder = gVal - x3 + itVal
        x3 = x3 + adder
        itVal = itVal + 1
        print(x1 , x2, x3, x4, x5, x6, x7, x8, x9, itVal)
        return()

      return()



    def valAdd():

      #for _ in range(1000000000):
        #a1()

      a1()
      a2()
      a3()



      print(x1, x2, x3, x4, x5, x6, x7, x8, x9, itVal)

      return()

    valAdd()

Having issue with small python script.小型 python 脚本存在问题。 trying to add 1 to a global variable every 3 iterations.尝试每 3 次迭代将 1 添加到全局变量。 I keep seeing我不断看到

"for 3 in scalerVal: ^ SyntaxError: cannot assign to literal" “对于 scalerVal 中的 3:^ SyntaxError:无法分配给文字”

I will appreciate an answer我将不胜感激

The problem is that you are using a constant where you should put a variable instead.问题是您正在使用一个常量,而应该在其中放置一个变量。

More specifically, in the python for loop, it should be something like this:更具体地说,在 python for 循环中,它应该是这样的:

for x in [scalerVal]

Actually, the first one should be a variable and the second one should be a iterable实际上,第一个应该是变量,第二个应该是可迭代的

3 needs to be a variable name such as X 3 需要是变量名比如X

for x in scalerVal:
  x + 1
  return()

Also, scalarVal should be a list or array, not a scalar for use with for.此外,scalarVal 应该是一个列表或数组,而不是用于 for 的标量。

You can just use the value directly.您可以直接使用该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM