Skip to content
Advertisement

Invalid syntax using += operator [duplicate]

I keep getting a syntax error when using += in python here is my code. I am also having troule with nonlocal. I am getting lots of errors including syntax errors and unbound local error Thanks in advance!!!

update: more code added this is all the code It now says local variable ‘citizens’ referenced before assignment

from time import sleep as slp
import time
import sys
def clear():
  print("33[2J33[H", end="")
def slow(text, endl="n"): 
    for c in text:
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(0.05)
    print(endl, end="")
def slow_in(prompt=''):
    slow(prompt, endl="")
    return input()

import random
def generate():
  import random
  slow("generating random circumstance...")
  slp(2)
  slow("done")


rd = random.randint
money = rd(1, 100)
health = 100
global citizens
citizens = rd(10000, 1000000)
supporters = rd(1,1000000)
def show():
  print("money-" + str(money))
  print("health-" + str(health))
  print("citizens-" + str(citizens))
  print("suppporters-" + str(supporters))

waysod = [""]

def death():
  wod = random.choice(waysod)
  rnum = rd(1,citizens//2)
  citizens -= rnum
  print(str(rnum) + " citizens died " + wod)
  return citizens











import random
rd = random.randint
slow("Welcome to presidential nightmare! ")
from time import sleep as slp
slp(.6)
slow("The easiest thing to do in this game is, well...")
slp(.8)
slow("destroy the country!!")
slp(.6)
slow("lets get started!!!")
slp(.6)
slow_in("press enter to continue...")
generate()
show()
death()

Advertisement

Answer

Three things with this code:

  1. the rnum assignment will fail because you are not doing integer division
  2. You actually want global not non-local in this case. Though you should avoid using global variables like this.
  3. You can do the second line as part of the first

This would be what I think you want:

from random import randint as rd
citizens = rd(10000, 1000000)
def function():
  global citizens  # Tells python you want the global value
  rnum = rd(1,citizens//2) # Uses the global value since python knows to do that
  citizens -= rnum   

Though I would recommend avoiding nonlocal altogether since it can cause many bugs down the road. I would suggest doing something like this stylistically:

from random import randint as rd
citizens = rd(10000, 1000000) # Initialize citizens to a random value

def reduce_citizens(citizens:int) -> int:
    """Reduces the number of citizens by up to half of the current amount"""
    rnum = rd(1,citizens//2)
    citizens -= rnum   # Since the value is being passed into the function directly we don't need global
    return citizens # give the new value back so it can be used

citizens = reduce_citizens(citizens) # Take the current value of citizens and remove some then reassign to citizens

The above code has the benefit of being usable with multiple sets of citizens, so for example:

from random import randint as rd
citizens = rd(10000, 1000000)   # Initialize citizens to a random value
citizens_2 = rd(10000, 1000000) # Initialize a second set of citizens to a random value
citizens_3 = rd(10000, 1000000) # Initialize a third set of citizens to a random value

def reduce_citizens(citizens:int) -> int:
    """Reduces the number of citizens by up to half of the current amount"""
    rnum = rd(1,citizens//2)
    citizens -= rnum
    return citizens

citizens = reduce_citizens(citizens) # Take the current value of citizens and remove some then reassign to citizens
citizens_2 = reduce_citizens(citizens) # Take the current value of citizens_2 and remove some then reassign to citizens
citizens_3 = reduce_citizens(citizens) # Take the current value of citizens_3 and remove some then reassign to citizens

Here is a good article and a good video on how scope works in python, but a good rule of thumb is to just keep everything you can local.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement