[Tutor] Help with return results statement.
Vusa Moyo
soweto at gmail.com
Thu Oct 22 05:31:22 EDT 2015
Thanks Alan.
Removed that .
=====
# Add your functions below!
def average(numbers):
total = sum(numbers)
total = 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
def get_letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
#Class average
def get_class_average(students):
results = []
for a in students:
b = float(get_average(a))
results.append(b)
return average(results)
# prompt for results.
students = [lloyd, alice, tyler]
print(get_class_average(students))
bother = get_class_average([lloyd, alice, tyler])
print(get_letter_grade(bother))
=====
Appreciate it.
Regards
Vusa
On Thu, Oct 22, 2015 at 11:14 AM, Alan Gauld <alan.gauld at btinternet.com>
wrote:
> On 22/10/15 10:03, Vusa Moyo wrote:
>
>> Hi Guys. Thanks for the responses and assistance.
>>
>> I came right after-all. The last 2 blocks read the following.
>>
>
> Glad you are happy but there is still something you could improve:
>
> def get_class_average(students):
>> results = []
>> for a in students:
>> b = float(get_average(a))
>>
>
> You really don't need the float() here. get_average() is guaranteed
> to return a float.
>
> results.append(b)
>> return average(results)
>>
>>
>> students = [lloyd, alice, tyler]
>> print(get_class_average(students))
>> bother = get_class_average([lloyd, alice, tyler])
>> print(get_letter_grade(bother))
>>
>> Worked like a charm.
>>
>> Thanks for the assistance.
>>
>> Regards
>>
>> Vusa
>>
>>
>> On Tue, Oct 20, 2015 at 2:38 PM, Alan Gauld <alan.gauld at btinternet.com
>> <mailto:alan.gauld at btinternet.com>> wrote:
>>
>> On 20/10/15 12:29, Vusa Moyo wrote:
>>
>> Hi there. My script is as follows,
>>
>>
>> alice = {
>> "name": "Alice",
>> "homework": [100.0, 92.0, 98.0, 100.0],
>> "quizzes": [82.0, 83.0, 91.0],
>> "tests": [89.0, 97.0]
>> }
>>
>>
>> # Add your function below!
>> def average(numbers):
>>
>> > total = sum(numbers)
>> > total = float(total)
>>
>> That line isn't necessary since the inputs are floats already.
>>
>> > total = 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
>>
>> def get_letter_grade(score):
>> if score >= 90:
>> return "A"
>> elif score >= 80:
>> return "B"
>>
>>
>> print get_average(lloyd)
>>
>> def get_class_average(students):
>> results = []
>> for a in students:
>> b = int(get_average(a))
>> results.append([b])
>> return results
>>
>>
>> get_class_average(alice)
>>
>> I receive a zero value for results, which doesnt quite make
>> sense to me.
>>
>>
>> Nor to me. Are you sure its a zero result? It should be a list of
>> some kind not a number. Or do you mean you get an empty list back?
>>
>> Notice that get_class_average() expects your students value to be
>> some kind of sequence or collection. The for loop will iterate
>> over that. If you pass Alice as a single student it will iterate
>> over the keys, trying first of all to get the average of "Alice"
>> which should fail with an error. Did you get any errors? If so
>> please let us see them.
>>
>> Please show us the actual code you execute, the actual output
>> and the full text of any errors.
>>
>> One other thing that seems weird to me is that you go to great
>> pains to produce a float as a result of get_average() but then you
>> immediately convert it to an int. Why not leave it as a float?
>>
>>
>> -- Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
More information about the Tutor
mailing list