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 8701255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:21:13+00:00 2026-06-13T02:21:13+00:00

Below is the code to compute the bill for the supermarket. Everything is ok

  • 0

Below is the code to compute the bill for the supermarket. Everything is ok but the issue is that I am told that this solution would not work if the input is only apple.

I do believe that the value of apple should be 0 since apples are not in the stock but still I believe there is something that I am not doing correct. Please help.

groceries = ["apple","banana", "orange",]

stock = {"banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
    }

prices = {"banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
    }

def computeBill(food):
    total = 0
    for item in food:
        tot = prices[item] * stock[item]
        print item, tot
        total += tot
    return total

computeBill(groceries)
  • 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-13T02:21:15+00:00Added an answer on June 13, 2026 at 2:21 am

    I am just going to go off on my own with this answer and make suggestions, since it seems the specifications for your computeBill functionality are not well defined.

    If the items are not in stock, and your instructor says it is not acceptable to return 0 in this case, then your other options are to raise an exception, or a sentinel value indicating an error state.

    def computeBill(food):
        total = 0
        for item in food:
            stock_count = stock[item]
            if stock_count == 0:
                raise ValueError("item %s is out of stock" % item)
            tot = prices[item] * stock_count
            print item, tot
            total += tot
        return total
    

    Or if you don’t want to raise an exception, you could return -1 if you feel that is not a valid total anyways:

            if stock_count == 0:
                return -1
    

    There are some other problems with the function in how it calculates the list vs stock, but you said you didn’t care about those right now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below code not work, but it's work fine for jsf1.2. Now the framework is
below code is my databasehandler class i got it from a tutorial. Beside that
In below code when I click 'Vote' a vote results screen is displayed but
The below code give me a foreach error. What appears to be the issue
note that the code below is obtained from Matlab documentation except for the bottom
What's wrong with this code?I'm currently calling the code below using a link. Is
I got the below code from @DanS at this link how-to-display-a-map-still-image-file-with-a-moving-current-location onCurrentPosition(Location current){ double
I am trying to compute using two loops. But I am not very familiar
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =

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.