Very confused


#1

Good evening everyone, I am stuck here. (Forever!!)

I don't understand the instructions, and I can't move forward. Help me !!!!!

link: https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/1?curriculum_id=4f89dab3d788890003000096#

    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}
#code starts here. :)
students=[lloyd,alice,tyler]
for student in students:
    for numbers in student:
        def average(numbers):
            total=sum(numbers)
            if total%1 ==0:
                total=total/ len(numbers)
            return total

Message from code academy:
Oops, try again. average([3, 0]) resulted in an error: maximum recursion depth exceeded


#2

We cannot define a function inside a loop. The function should be independent of the rest of the program.

def average(grades):
    return float(sum(numbers)) / len(numbers)

Technically, since the numbers are already floats, we should be able to write,

def average(grades):
    return sum(numbers) / len(numbers)

Check if it works, and if not, use the first example.


#3

thank you very much mister !!!!
I love you. :cry:

Let me put my focus helmet on so that everything makes sence, but already you've gave me good information. :smiley:

Edit 1: Python doesn't say anything about the for loops being used first. maybe
it's because I didn't put print or something?

Edit 2:
Oops, try again. average([3, 0]) resulted in an error: not all arguments converted during string formatting

My new code:

[lloyd,alice,tyler]

def average(numbers):
    for student in students:
        for numbers in student:
            if sum(numbers)%1==0:
                total=sum(numbers)
                total=float(total)
                total=total/ len(numbers)
                return total    
        print total

I am not sure I understand the instructions if you care to explain. :slight_smile:


#4

Instructions:

Write a function average that takes a list of numbers and returns the average.

Define a function called average that has one argument, numbers.

Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total.

Like the example above, use float() to convert total and store the result in total.

Divide total by the length of the numbers list. Use the built-in len() function to calculate that.

Return that result.

If you examine the samples I posted earlier and check then against these instructions you will find that your code does way too much, and should not even have a loop (unless you wish to manually add up the grades).

def average(numbers):
    total = 0
    for grade in numbers:
        total += grade
    # test line - total should be a float
    print total
    # remove above line after confirmation
    return total / len(numbers)

We can confirm that total is indeed a float; however, the SCT is actually looking for the keyword float in our code, so we must write the last line as,

    return float(total) / len(numbers)

#5

Thank you mtf for your reply. Sorry I didn't answer earlier.
You didn't use the build-in function sum().

I have a question, can I take off a "x" in a for loop. (for x in A:)?

I copy pasted your code. it worked. But, when I changed
total+= grade
to
total= sum(grade)
Same error. How can I take off the names from the for loop.


#6

That was test code, not solution, for the record. It was to test for floats in the normal data stream, and prove it was not necessary in the code (though expected, if I remember correctly).

The variables we use in for statement are completely arbitrary. Descriptive names are best, but in small helper utilities I often resort to single letter variables, so long as I identify the data in the call arguments. Am I reading your question correctly?

The for loop is not needed if we use the built-in sum() function. It invokes an iterator internally.


#7

I am still not too sure how this works; I would love to see the actual built-in function sum() for clarity.

That being said, you have helped me a ton. I understand a little bit better what I did wrong (Which is huge). I am ready to skip this lesson (I think), hopefully the next lessons will give me more intel about this.


#8

"def" is to define a function. and "For" is a loop operation. The average function should look, like this:

def average(numbers):
    for item in numbers:
        total = float (sum(numbers))
        return total / len(numbers)

See "for" is still inside the "def" (function) not the other way. "numbers" are inputs for the function. So when you will next use this function to the next exercise:

    def get_average(student):
        return 0.1 * average(student["homework"]) + \
        0.3 * average(student["quizzes"]) + 0.6 * average(student["tests"])

Now, we are using average function inside another function. So, you can use a function inside another function to make a new one but you cannot put a function(def) inside a loop operation(for).

To get the average first I need the sum so I did:

total = float (sum(numbers))

I could just simply write sum(numbers) which is correct syntax wise but I use float() to get a float result.

Float numbers are simply say decimals like 1.0 or 3.5..etc and integers are whole numbers(positive or negative). If you divide say 6 with 4 and the answer is 1.5 the computer might give you 1(whole number) as you did not define numerator and/or denominator as float. And you are likely to get a wrong answer.

So, it's a good idea to define numerator (in this case) as float to avoid that confusion.