Python 101 Function Duck, Duck, Goose

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
Assume that you are working at a poultry farm, counting animals that are traveling down a conveyor belt (don’t ask). Each duck is worth 1 point. Geese are larger, so they are worth 2 points each. Eggs reset your current count to 0. For example, the sequence [“duck”, “duck”, “goose”] would produce the count value 4 (1 for each of the ducks, plus 2 for the goose). [“duck”, “goose”, “egg”, “duck”] returns 1 (the egg resets the earlier count to 0, so only the last “duck” contributes to the final score).
Complete the ddg() function, which takes a list of strings as its only argument. Each string value will be either “duck”, “goose”, or “egg” (all lowercase). The function returns a non-negative integer according to the rules above.

Function Call
Return Value
ddg([“goose”, “duck”, “duck”, “duck”])
5
ddg([“duck”, “egg”, “goose”, “egg”])
0
ddg([“duck”, “duck”, “egg”, “goose”])
2<In what way does your code behave incorrectly? Include ALL error messages.>

I’m not sure how to write out the function please help!!
<What do you expect to happen instead?>

```python

Replace this line with your code.

<do not remove the three backticks above>

Let’s plan this in stepwise fashion.

The function’s name is ddg. It will have one parameter, which is the list of animals. We could name it animals. Given those specifications, what would the function header look like?

1 Like

Remember that a function definition begins with def, so that should be the first thing on the first line. A parameter is a variable, so it should not be surrounded by quotes.

1 Like

Actually …

def ddg(animals):

Now, we need have a variable to hold the total number of points, and initialize it to 0. We could call it points. Based on that, what should be the second line in the function?

1 Like

Are you familiar with assignment statements?

1 Like

Yes.

We need to assign 0 to a variable named points. What would the statement be for that?

EDITED (February 16, 2017) to add the following …

After the initialization of points, create a for loop that iterates through animals.

Within the loop, put an if-elif-else `` structure that identifies each item within animals as either a "duck", "goose", or "egg", and adds the appropriate number of points to the points variable or resets it to 0, as appropriate.

After the loop …

        return points

Let us know how this works out for you.

1 Like

Hello,

I’m also working on this problem. Thank you for your assistance so far. For this assignment we were given a bare bones file that contains function stubs and a few tests to see if our code is correct. The file already contains def (poultry):. From what I understand we need to assign 0 to the points variable because as we assign points to duck, goose, and egg the output will be determined based on the value of each? Could you walk me through this process please? If duck is work 1 point, goose 2, and egg “resets” the amount of points in the sequence how does that translate in an if-elif-else structure?

def(poultry):
points = 0
if points += 1
return ducks
if points += 2
return goose
if points = 0
return egg
I’m using PyCharm and I am completely new to this subject.

Thank you in advance for any help.

The problem looks like this and the code below is what I came up with so far but it tests “NONE”:

In this part of the file it is very important that you write code inside
the functions only. If you write code in between the functions, then the
grading system will not be able to read your code or grade your work!

def ddg(poultry):
# Write your code here and edit the return statement below as needed.
poultry = [‘duck’, ‘goose’, ‘egg’]
def sum(poultry):
points = 0
for duck in poultry:
points == 1
return points

Hi @kas214,

With a function header and an assignment statement that initializes the points variable to 0, we have this …

def ddg(poultry):
    points = 0

Now we need a for loop to iterate through the items in the poultry `` list. The loop header could be …

    for item in poultry:

As described above, within the loop, we need an if-elif-else ``structure that identifies each item within poultry as either a "duck", "goose", or "egg", and adds the appropriate number of points to the points variable or resets it to 0, as appropriate. How would you write such a structure? Give it a try and post your code so that we can refine it, if necessary.

EDITED (February 16, 2017) to add the following …

The following Codecademy Python courses may be helpful to review for this project …

Hi, @devsolver53757 and @kas214.

Prior to writing your code, plan the process for solving the problem, then write the code to implement that plan.

This project involves three main steps, with the second step being the most complex. In this project, we are keeping a tally of points. All of the steps involve managing that tally …

  • Step 1: Start the tally of points at 0.
  • Step 2: Iterate through the list of items, updating the tally of points based on the value of each item.
  • Step 3: Make the final tally of points available to the caller of the function.

Following is a skeleton of a ddg function for implementing these steps. Within the function, three of the lines are …

    pass

We have a variable named points that will hold the tally of points. The pass command is a stand-in that does nothing. As it is now, the function can execute, but points stands at 0. Your task is to replace each of the pass commands with a statement that updates points appropriately.

Good luck! Let us know how this works out for you.

def ddg(poultry):
    # Start the tally of points at 0.
    points = 0
    # Iterate through the list of items,
    # updating points based on the value of each item.
    for item in poultry:
        if item == "duck":
            # Replace the next line.
            pass
        elif item == "goose":
            # Replace the next line.
            pass
        elif item == "egg":
            # Replace the next line.
            pass
    # Make the final tally of points available to the caller of the function.
    return points

print(ddg(["duck", "duck", "goose"]))
print(ddg(["duck", "duck", "egg", "egg"]))
print(ddg(["duck", "egg", "goose", "egg"]))
print(ddg(["duck", "duck", "egg", "goose"]))
print(ddg(["goose", "goose", "goose", "goose"]))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.