12.stocking out


#1



Replace this line with your code.

Hello!
This is right answer, but I wonder why I right.
I don`t understand why I remove just one in stock on line 23.
I think remove number following what people want.
The number can change anytime.
Please give me explanation.


#2

Hi @jeongmilee,

I think it is because later on when you call the function, you will be passing the list shopping_list into the function. Since you can't type like shopping_list = ["2 bananas", "3 oranges", "5 apples"], when indicating multiple items, you probably have to have the item appear multiple times in the list like this: shopping_list = ["banana", "banana", "orange", "orange", "orange", "apple", "apple", "apple", "apple", "apple"] or some other way which I have also yet to learn about yet :stuck_out_tongue:

Since each item in the list has a quantity of one, it is safe for the stock to decrease by 1 each time the list is looped over, for each and every item in the list.

Hope this helps :slight_smile:


#3

@jeongmilee,

In this exercise you have to understand

  • the function talk
    as you are defining a compute_bill() function
    which takes 1 parameter food
    -
    As you call the compute_bill() function
    and the compute_bill() function was defined as having 1 parameter
    you will have to provide the compute_bill() function with 1 argument
    in this case a =list= Value shopping_list
    -
    But as you use the return statement
    the compute_bill() function will return a **number_ Value** with NO-Display
    you will have to call the compute_bill() function like
    print compute_bill(shopping_list)

  • the description of a dictionary
    like the stock dictionary
    which consists of 4 KEY - VALUE-pairs ( so called properties )
    a banana property with property-key banana and an associated _number Value 6
    an apple property with property-key apple and an associated number Value 0
    an orange property with property-key orange and an associated number Value 32
    a pear property with property-key pear and an associated number Value 15
    -
    To get access to the associated Value of a property
    you will have to use the so-called bracket notation
    where you will place a property-key as a string within those brackets
    like
    stock["banana"] with which you are accessing the number Value 6

  • the shopping_list =list= consisting of 3 list-Element each having a string Value
    A FOR IN loop will iterate over the =list= at every iteration returning 1 Element
    in this case you would iterate 3 times

Thus i would write the compute_bill() function as

shopping_list = ["banana","orange","apple"]
stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:
            total = total + prices[key]
            stock[key] = stock[key] -1
    return total

print compute_bill(shopping_list)

#4

Is it a good idea to change global variables, in this case stock? If the variable is used in another part of the programme it will arise problem. For example, if the instruction also asked for comparing the original stock after a purchase has been made? I would normally won't change a global variable.

By the way, I was wondering if there is a function talk or something similar for Ruby. Would help me a lot.


#5

@jeongmilee,

### the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )

#6

def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total = total + prices[item]
stock[item] = stock[item] - 1
return total


#7

shouldnt you call shopping_list as opposed to compute_bill since it changes stock?