Ask coding questions

← Back to all posts
( Economy data - Replit DB?
meziosaur (0)

I needed some guidance, I wanted to save 4 kinds of currencies per user into a replitDB.

I'm kinda lost and not sure where to start with it.
Would I need a key per user like so:

It's this the right train of thought?

OldWizard209 (1631)

You will need to save a dictionary as a value to the username's key"

from replit import db

user_name = "SomeUsername" # Put user's name to variable
user_amount_usd = 1000         # Put user's amount to variable

db[user_name] = {
    "amount_economy": {
        "dollar_amount" : user_amount_usd,
        "pounds_amount" : user_amount_usd*0.72,  # Converting US Dollars to UK Pounds.
        "indian_rupees" : user_amount_usd*74.23, # Converting US Dollars to Indian Rupees.
        "chinese_yuan" : user_amount_usd*6.46    # Converting US Dollars to Chinese Yuan.

user_amount = db[user_name]["amount_economy"]

print(f"{user_name}'s amount in Pounds is {user_amount['pounds_amount']}")
Coder100 (18915)

How about this:

db[user id] = {
  'currency1': ...
  'currency2': ...
  'currency3': ...

remember to treat it like a dict.

OldWizard209 (1631)

Are you sure you can access a value in a dictionary in Python using a dot notation? You are using the concept of JavaScript Objects in this example:


Coder100 (18915)

well it was a semantic example after all @OldWizard209

Coder100 (18915)

but from what i can tell id is a property of the class user @OldWizard209

OldWizard209 (1631)

Well if it is a property then you were right but as a general example, it should be best to do db[user][id]. Also, the user(question asker) said they were using multiple currencies so it would be better to save a dictionary as a value in the db, containing all the currencies.

Coder100 (18915)

yes that is what i did did you not see @OldWizard209