5
Ashuk
7d

Hi,
So I'm learning python now days and I'm stuck at this topic <functions>
I'm using the book Learn Python3 the hard way (3rd Edition)

In the following code there is a function called cheese_and_crackers and two parameters called cheese_count and boxes_of_crackers, what is happening? i mean this code looks so confusing can anyone explain this to me. Please. Thank you in advance and sorry for the trouble.

Code:
1 def cheese_and_crackers(cheese_count, boxes_of_crackers):
2 print(f"You have {cheese_count} cheeses!")
3 print(f"You have {boxes_of_crackers} boxes of crackers!")
4 print("Man that's enough for a party!")
5 print("Get a blanket.\n")
678
print("We can just give the function numbers directly:")
9 cheese_and_crackers(20, 30)
10
11
12 print("OR, we can use variables from our script:")
13 amount_of_cheese = 10
14 amount_of_crackers = 50
15
16 cheese_and_crackers(amount_of_cheese, amount_of_crackers)
17
18
19 print("We can even do math inside too:")
20 cheese_and_crackers(10 + 20, 5 + 6)
21
22
23 print("And we can combine the two, variables and math:")
24 cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

Output:

We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.
OR, we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.
We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.
And we can combine the two, variables and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.

Also, Can a function's parameter can be called with different names. like a function with parameters first_name , can be called by using f_name?

Comments
  • 2
    yes,
    here's an example

    def hello(first_name):
    f_name = first_name
    print(first_name)
    print(f_name)
    // same thing
  • 1
    What is confusing exactly? I am reading the same book

    The dude's recommendation is to comment above each like of code what you think is happening, did you try that?
  • 2
    yes you can call function parameters with different names, but that would be pointless. i mean you can create something easier in the first place, for example :

    def make_fullname(fn, ln):
    return "Your name is : {}".format(fn, ln)

    fn would be first name, and
    ln would be last name.
  • 3
    1.- Please tell us what is exactly that is confusing you? Which part you find more troubling understanding? (For this you'd need to read the entire thing line by line and determine which line makes no sense for you and which does)

    2.- Your last question, yes. It is called variable scope. Basically a variable has a "space" where it exists. For a function, a parameter 'param' only exists inside the function and it takes whatever value you pass when you call the function:

    example

    def print_number(my_number):

    print(my_number)

    ...

    print_number(5) # my_number will take value 5

    ...

    my_value = 9 # I created a variable named my_value and equal to 9

    print_number(my_value) # my_number will take my_value's value which is 9
  • 1
    1. Defines a new function called chese_and_crackers that takes 2 arguments: cheese_count and boxes_of_crackers.
    2. The "f" before first " makes a formated string, meaning that it will replace every "{cheese_count}" with value of the variable cheese_count. print(...) displays that string on screen

    3, 4, 5 do the same. "\n" means a new line (basicaly it presses enter)

    9. Calls that function and gives it hardcoded values, in this case 20 and 30, so in the function cheese_count = 20, boxes_of_crackers = 30

    12 - 16. Makes some variables to store the value and calls the function with those variables instead of hardcoding numbers.

    20, 24. Same, but it calls it with result of math operations.

    Hope it helps
  • 1
    @andr3w000

    @wowotek

    @antorqs

    @killermenpl

    Thank you guys for the explanation and tips. :)
  • 0
    @mundo03 Yes I'm doing that and i can't comment on the lines which i don't understand :/
  • 0
    @mundo03 @antorqs

    This is confusing me.

    Code:

    print("OR, we can use variables from our script:")

    amount_of_cheese = 10

    amount_of_crackers = 50

    cheese_and_crackers(amount_of_cheese, amount_of_crackers)

    output:

    OR, we can use variables from our script:

    You have 10 cheeses!

    You have 50 boxes of crackers!

    He created 2 new variables and give them numerical value and then use them as arguments and call the function . right?

    And this

    code:

    print("And we can combine the two, variables and math:")

    cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

    Output:

    And we can combine the two, variables and math:

    You have 110 cheeses!

    You have 1050 boxes of crackers!

    And this one is linked to the above one, am i right?

    That's all.It's been a year since i code so i thought i should utilize my free time learning something new.
  • 1
    @Ashuk I think you got it. That's what the code does.

    Creates two variables and use them as arguments, so whenever the function is called, the current value of the variable is passed as arguments. And also you can apply math to them.
  • 0
    @Ashuk the point of this exercise is to show how you can pass variables to a function.

    The examples use a vale as argument, variables a argument, even operations as arguments.

    By your comments, I think you got it, just keep going even if you don't fully understand the exercise, but do all the drills.
  • 1
    @mundo03 Thanks Man :)
  • 0
Your Job Suck?
Get a Better Job
Add Comment