Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – Config.json number not counting as a number

Hello im coding a program for hypixel skyblock minecraft that tells me a price of items that can be sold for a higher price, the program is done but im running into a issue. I have made a config.json file and it contains information like discord webhook lowest price and to notify the user if a item is found. Im having problems with the lowest price function, basically when i put into the config lowest price and i put the value to 1 the program doesnt work and gets a traceback. The traceback doesn’t matter because i know the issue. When i put the number into config it doesnt work but when i set lowestprice = 1 in program manually it works like when i put the number into config the program thinks maybe the number is a text or something. heres the config code

with open("config.json", "r") as f:
    data = json.load(f)
webhookread = data["webhook"] 
notifyread = data["notify"]
lowestpriceread = data["lowestbin"]

WEBHOOK = webhookread
LOWEST_PRICE = lowestpriceread - THE ISSUE
NOTIFY = notifyread

is there a way i could make the config file put the number as a real number not a text or any of that? so i can still use the config for numbers?

==========================
Thank you for helping me! it works now

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Are the numbers in the json file stored as strings or as numbers? if the json looks like this:

{
  "lowestbin" : "123.45"
}

then the price is saved as string and will need to cast to a float type. This is simple to do:

lowestpriceread = float(data["lowestbin"])

Note: this code will thrown an exception if the data in the json cannot be converted into a float.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading