6. Just Weight and See


#1

I'm really confused about what I've done wrong, it says I'm trying to times an integer by a float, but I don't really understand why...

lloyd = {
"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]
}

Add your function below!

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

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

`


#2

There are two types of "number" variables in python.Float, and integer.
Float includes a decimal: 3.2
Integer is a whole number: 2

To fix your code, simply make sure all your number variables are set to float, and you shouldn't have an issue.


#3

You have defined homework (in this case the average of the homework scores) as a list when you do this:

homework = [........]

It is a number (float) and doesn't need to be in a list.

Simply define

homework=average(student["homework"])

Like in my code below:

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


#4

Yeah, I got it to work just by taking out the extra square brackets in the last section... I don't really understand what that has to do with the float thing


#5

when you define
homework = [......]
You actually create a list that contains the value calculated by ///**average(student["homework"])**///

You cannot multiply a list by a float. For example, if your average was 80.85
ie homework = [80.85]
>> homework*2
will give you [80.85, 80.85]

However

>>homework*0.1
will give an error because you are simply telling Python to divide the list to a tenth

I hope that answers your question as to the source of the "float"


#6

Okay, sorry if I'm being really silly, but if the items in the list are the average and not an integer, surely you can multiply them by a float? Is it because some of the values might be integers?


#7

Python doesn't multiply each element by X, where X is a given value, but it multiplies each element X times.
If you try to multiply a list with a float, sure won't work but Python has its ways, and even though is a slightly more advanced topic, you can use the numpy module to do the trick (it's fun but wouldn't suggest using it unless you really need it).
Of course the data type will change from list to numpy.array, or something similar, but it works too.

If you want to have float values on your list, try getting each element, multiplying by your float and appending to a new list.
That sure would work.

Edit: the above explanation is just for the case where the programmer wants to turn all the integer values on a list to float values (either by playing with numpy or working with appending each element to another list). In the case of this exercise, there won't be such need since all we'll need to do is to get the average of a list and multiply it.


#8

This is even simpler,I guess :stuck_out_tongue:
def get_average(student):
homework=0.10*average(student["homework"])
quizzes=0.30*average(student["quizzes"])
tests=0.60*average(student["tests"])
return homework+quizzes+tests


#9

I am sorry to disturb but I really want to know why my code isn't able to run even it is like u....


#10

lloyd = {
"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]
}

Add your function below!

def average(numbers):
total = 0
total+=sum(numbers)
return float(total)/(len(numbers))

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


#11

I just don't know what' wrong with my code ..it really confused me..


#12

The only part that I wrote that is different from you is the def average(number) part. The following is mine and it works!

def average(numbers):
total=sum(numbers)/len(numbers)
return total


#13

However,it still doesn't work....Oh my god..I am almost go mad..


#14

That's strange. Perhaps you can check whether you had typing errors? or are there spaces(tab) that you had missed?
I have stuck in this part for a while, so I checked parts by parts to see if there is any typing errors. Hope you can find out the error soon!


#15

I know where I was wrong.That's too stupid.It is the " : ",and I type it with Chinese character..Aha!I did find the error! And thank u all the same for your kind help.


#16

Having the same freaking error and there is not one part of the code I can tell is wrong.

lloyd = {
"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]
}

Add your function below!

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

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])

return sum((homework * 0.1) + (quizzes * 0.3) + (tests * 0.6))

#17


#18

lloyd = {
"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]
}

Add your function below!

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

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

Working


#19

thx a lot varunks, best solution for this task


#20

A post was split to a new topic: 6. just weight and see