#🔒 Issue with a TypeError: unsupported types for __add__: 'NoneType', 'float'

20 messages · Page 1 of 1 (latest)

summer goblet
#

So I'm doing a little object oriented experiment with dictionaries and behaviors, but I've ran into this problem.

This is the function where it happens, in Line 49.

def _step_enemy(obj):
    print(environment[obj].get('cnt'))
    environment[obj].__setitem__("cnt", environment[obj].get('cnt') + 0.1)
    if environment[obj].get('cnt') < 20:
        if environment[obj].get('posy') == 4:
            del environment[obj]
        else:
            if environment[obj].get('posy') < 4:
                environment[obj].__setitem__("posy", environment[obj].get('posy') + 1)
    else:
        environment[obj].__setitem__("cnt", environment[obj].get('cnt'))

This is what the stuff above looks like.

#Entorno de los objetos
environment = {"Player":{"posx": 2, "posy": 2}, 
               "Enemy":{"posx": 4, "posy": 0, "cnt": 0.0 } 
              
              
              }

# Funciones basicas del engine

def gfx():
    display.clear()
    for obj in environment.items():
        #  Obtener posiciones y graficar en pixeles
        v1 = obj[1]["posx"]
        v2 = obj[1]["posy"]
        display.set_pixel(v1, v2, 9)


def log():
    # Array para determinar si no hay enemigos
    checkarray = []
    
    for obj in environment:
        if obj == "Enemy":
            checkarray.append(obj)
            _step_enemy(obj)
            
            
    if len(checkarray) == 0: # Si no hay enemigos
        # Crear enemigos
        environment["Enemy"] = {"posx": random.randrange(0, 4), "posy": 0}
shrewd cometBOT
#

@summer goblet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

summer goblet
#

what I expect to happen is to have some delay for the enemies to get to the botttom, instead of lowering the number once every cpu cycle which ends up chaotic at best.

#

Instead it starts out okay but quickly errors after it gets to 0.5 and for the life of me I can't figure out why or what the error even implies.

pine sentinel
#

is there a default value that makes sense mathematically?

summer goblet
summer goblet
#

I'm not sure how you set a default though

pine sentinel
#

you can do environment[obj].get('cnt', 0)

#

and then 0 will be the default instead of None

summer goblet
#

oh

#

thanks

summer goblet
summer goblet
# pine sentinel you can do `environment[obj].get('cnt', 0)`

so, the way my logic is supposed to work is that it waits a certain amount of time before running the code to go down, so it doesn't just go everywhere erratically at crazy speeds.
however, what happens is it waits initially and then just swoops the rest of the way down disregarding speed.


def _step_enemy(obj):
    tiempo = environment[obj].get("cnt")
    environment[obj].__setitem__("cnt", tiempo + 0.1)
    print(tiempo)
    if tiempo >= 200:
        if environment[obj].get('posy') == 4:
            del environment[obj]
        else:
            if environment[obj].get('posy') < 4:
                environment[obj].__setitem__("posy", environment[obj].get('posy') + 1)
    elif tiempo > 200:
        environment[obj].__setitem__("cnt", 0)
        
#

tiempo is like, time

#

after tiempo reaches the right number it just immediately goes down instead of progressively going down

#

it's not just it setting itself to the lowest pixel because sometimes you can actually catch the inbetween

shrewd cometBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.