Python Forum
Subtracting values between two dictionaries/ floating point numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subtracting values between two dictionaries/ floating point numbers
#1
Hello everybody hope your all doing great :D,

I am new to learning python and challenged myself to make a small program for a game i like to play called Star Citizen, the purpous of the program is to take two inputs, player location and player destination. It will then take those two sets of data and calculate from a dictionary what the user can buy at their current location and sell at their destination and what would be the most profitable.

I am running into an issue when it comes calculating this. I believe im probably going about this all wrong so please don't worry about telling me to start from scratch with different ideas.

Here is the code so far:

#### taking user information
user_purchase_location = input("Were are you planning to buy your goods?").lower()
user_sale_destination = input("Where are you planning to sell your goods?").lower()

#### dicts of items available to buy and sell at locations along with UEC price.
buying_olisar = {'Medical Supply': 17.01,}
selling_olisar = {'Agricium': 25.60, 'Aluminum': 1.25, 'Beryl': 4.26, 'Chlorine': 1.57, 'Corundum': 2.53, 'Diamond': 6.90, 'Distilled Spirits': 4.95, 'Fluorine': 2.80, 'Gold': 6.07, 'Hydrogen': 1.02, 'Iodine': 0.41, 'Laranite': 28.91, 'Processed Food': 1.39, 'Quartz': 1.44, 'Scrap': 1.67, 'Stims': 3.40, 'Titanium': 8.27, 'Tungsten': 3.90,}
buying_levski = {'Agricultural Supply': 1.11, 'Aluminum': 1.20, 'Hydrogen': 0.98, 'Iodine': 0.38, 'Quartz': 1.37,}
selling_levski = {'Agricium': 25.60, 'Altruciatoxine': 11.63, 'Beryl': 4.25, 'Chlorine': 1.56, 'Corundum': 2.53, 'Diamond': 6.07, 'Distilled Spirits': 4.95, 'Fluorine': 2.80, 'Gold': 6.07, 'Laranite': 28.25, 'Medical Supply': 18.00, 'Processed Food': 1.38, 'Scrap': 1.68, 'Stims': 3.40, 'Titanium': 8.27, 'Tungsten': 3.90, 'Widdow': 24.00,}

#### Sorting the dict into ascending value instead of alphabetical
sort_buying_olisar = sorted(buying_olisar, key=lambda tup: tup[1])
sort_selling_olisar = sorted(selling_olisar, key=lambda tup:(tup[1], tup[0]))
sort_buying_levski = sorted(buying_levski, key=lambda tup: tup[1])
sort_selling_levski = sorted(selling_levski, key=lambda tup:(tup[1], tup[0]))


#### picking two dicts (buy/sell) and filtering them to print only common elements.
def common(curr, other):
    return set(curr.keys()).intersection(set(other.keys()))
tradable_items = common(buying_levski, selling_olisar)


#### calculating the prices between possible sales.
prices = {k: selling_olisar[k] - buying_levski[k] for k in tradable_items}


print(prices)
When i run this the figures are coming out as floating point numbers for example:

Quote:Were are you planning to buy your goods?levski
Where are you planning to sell your goods?olisar
{'Quartz': 0.06999999999999984, 'Hydrogen': 0.040000000000000036, 'Iodine': 0.02999999999999997, 'Aluminum': 0.050000000000000044}

What i a can't figure out is how to #1 have these floating point numbers be not so large and #2 how to then print out the item in the dictionary that holds the largest number.

Any guidance would be massively appreciated.

Thanks any questions please ask and sorry if the formatting isn't perfect i am a really new to this :).
Reply


Messages In This Thread
Subtracting values between two dictionaries/ floating point numbers - by FloppyPoppy - Mar-03-2019, 06:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with subtracting values using SQLite & Python Extra 10 3,400 May-10-2022, 08:36 AM
Last Post: ibreeden
  Subtracting datetimes [index] Mark17 2 2,478 Aug-21-2021, 12:11 AM
Last Post: Larz60+
  5 variants to invert dictionaries with non-unique values Drakax1 2 2,611 Aug-31-2020, 11:40 AM
Last Post: snippsat
  Accessing values in list of dictionaries pythonnewbie138 2 2,133 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138
  Indexing problem while iterating list and subtracting lbtdne 2 2,136 May-14-2020, 10:19 PM
Last Post: deanhystad
  floating point not increasing properly rakeshpe43 4 2,401 Apr-30-2020, 05:37 AM
Last Post: rakeshpe43
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,598 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  Complex floating issue arshad 2 11,838 Nov-05-2019, 03:26 PM
Last Post: arshad
  floating point arithmetic exDeveloper 2 2,109 Sep-25-2019, 04:33 PM
Last Post: DeaD_EyE
  finding the closest floating point number in a list Skaperen 17 8,283 Sep-19-2019, 10:39 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020