Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:14:44+00:00 2026-06-12T01:14:44+00:00

My goal for this function is for it to output the grand total cost

  • 0

My goal for this function is for it to output the grand total cost for an order.

I have one function that gives me a dictionary like {'2': '150.99', '3': '99.50', '15': '5.07'} where the first value is an ID number and the second value is the price for the item with that ID number. So item number 3 costs 99 Dollars/Euros/Pounds/Whatever my units are and 50 cents/subunits.

Another function gives me a list of item IDs to look up like this [2, 2, 3] where any item ID can appear more than once to show that I am purchasing more than one of that item. So in this example I’d be buying 2 of item 2 at 150.99 each and 1 of item 3 at 99.50.

I want to define a function that lets me pass it the dictionary of item ID: prices and the list of item IDs to lookup and it would output the total cost for all items in the list. So for the above example it should output the value 401.48 because (2*150.99)+99.50=401.48.

As a test I have tried

test = DictionaryVariable.get('2')
print test

This prints the expected value (so in this example it’d be 150.99.)

However, my attempts to define a function like

def FindPrice(dictionary, idlist):
    price = 0.00
    for id in idlist:
        price + float(dictionary.get(id))
    return price

and then call it like this:

Prices = FindPrice(DictionaryVariable, IDListVariable)
print Prices

have not worked – the dictionary.get(id) part seems to be returning None (which I have tested by making it a str instead of float) so it throws an error saying “TypeError: float() argument must be a string or a number”. I suspect that I need to use some other method of feeding in the item IDs from my list, but I don’t know what that method would be. I am not sure why the dictionary.get(id) returns None but yet DictionaryVariable.get('2') gives it the value I’d expect.

EDIT:
Thanks to the helpful posts that pointed out that I was using strings in my dictionary rather than integers or floats I got this to work:

def FindPrice(dictionary, idlist):
    Price = sum(float(dictionary.get(x,0)) for x in idlist)
    return Price
Prices = FindPrice(DictionaryVariable, FunctionThatMakesListofIDs('FileToGetIDsFrom.xml'))
print Prices
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:14:46+00:00Added an answer on June 12, 2026 at 1:14 am

    Your list [2, 2, 3] contains integers, but the keys of your dict are strings. I propose you create your dict using int keys and float values to begin with, or convert it like this:

    >>> d = {'2': '150.99', '3': '99.50', '15': '5.07'}
    >>> d = dict((int(k),float(v)) for k, v in d.iteritems())
    >>> d
    {2: 150.99000000000001, 3: 99.5, 15: 5.0700000000000003}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function. And its goal is to take the last character first
I have data like this: Team | goal a | 5 b | 8
My goal was to have a template function that would take as an input
my goal is if I have this: colmuns c1 | c2 | c3 |
Here's my ultimate goal in all of this. I have a viewcontroller with a
My goal is to have the url routing as following: http://www.abc.com/this-is-peter-page http://www.abc.com/this-is-john-page What is
I have this function, the input is the var cmd for example a dmesg
I have a console application that prints on standard output. I want to implement
I have a jQuery plugin: (function( $ ){ $.fn.test = function(selector){ $this = this;
I have the following function that decodes a URL into a hash comprised of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.