Ask coding questions

← Back to all posts
Error when accessing replit database in python
h
ZoutigeWolf (2)

Whenever i try to access my database with value = db["key"] I get the error TypeError: unexpected type "float".

This never happened before, can someone tell me what this means?

the file with problems = cogs/xp.py

Comments
hotnewtop
Coder100 (18187)

your repl is private so we can't see your code.

ZoutigeWolf (2)

@Coder100 thx for letting me know, i set it to public

Coder100 (18187)

i see, what are you exactly trying to do here?

@ZoutigeWolf

the types are already correct, and no type will ever need to be converted. Also, these values don't appear to be valid dict values.

ZoutigeWolf (2)

@Coder100 same error happens when i don't do the dict() it was just a test i forgot to remove

ZoutigeWolf (2)

@Coder100 any line where i want to get a value from the database
example: xpDB = db["xp"]

Coder100 (18187)

well i mean you put the dict function on all of them so you should remove that @ZoutigeWolf

ZoutigeWolf (2)

@Coder100 I did and it still errors

ZoutigeWolf (2)

@Coder100 for me it shows multiDB = db["xpMulti"]

Coder100 (18187)

strange maybe the file didn't save @ZoutigeWolf

ZoutigeWolf (2)

@Coder100 I found the solution, apparently a dict in the database can't have a float as a value.
The xp is normally an int but the multiplier made it a float so i now round that to an int.
And I now store the multipliers as a string which i can later convert back to a float

xfinnbar (82)

@ZoutigeWolf Replit DB is based on JSON and it doesn't support anything other than strings as keys.

ZoutigeWolf (2)

@xfinnbar ohhhhhhh that makes a lot of things clear.
but when i stored info on my own pc in JSON for a project I was able to store integers and floats

xfinnbar (82)

@ZoutigeWolf They were converted to strings tho.

Coder100 (18187)

no that's ridiculous json can have numbers as the key

however, dicts cannot. @xfinnbar

Coder100 (18187)

nice solution
yeah I thought the python db would get all these nitty types down for you, but I guess not @ZoutigeWolf

ZoutigeWolf (2)

@xfinnbar i had keys as strings and values as floats / ints in my json, they werent converted

Coder100 (18187)

in json, floats and ints are indistinguishable and everything is a float

@ZoutigeWolf