Re: [Tutor] Working collaboratively
On 2015-10-20 01:02, Alan Gauld wrote: On 20/10/15 07:33, Alex Kleider wrote: Look closely at what the return value is called in each case. And see how it compares to the names in the signature. OOPS! Should have run over them with diff _before_ posting rather than after. Sorry about that. Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with return results statement.
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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with return results statement.
Hi Vusa, and welcome. On Tue, Oct 20, 2015 at 01:29:59PM +0200, Vusa Moyo wrote: > Hi there. My script is as follows, [...] > def get_class_average(students): > results = [] > for a in students: > b = int(get_average(a)) > results.append([b]) > return results I'm not sure why you are getting 0, but the get_class_average function is definitely wrong. The problem is in the structure of the function. You have the *return* inside the for-loop, which means that only the first student will ever be processed. As soon as the function gets to the "return results" line, it will exit the for-loop leaving everything else unprocessed. You need to unindent the return so it looks like this: def get_class_average(students): results = [] for a in students: b = int(get_average(a)) results.append([b]) return results Now the for-loop will run all the way to the end, and the function will only return at the very end. I'm also not entirely sure about the [b] argument to append. Are you sure it is supposed to be [b]? That will be appending a *list* consisting of a single value. I think you want: results.append(b) without the square brackets. Why don't you try both and see the difference? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with return results statement.
Hi there. My script is as follows, 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) 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" print get_average(lloyd) def get_class_average(students): results = [] for a in students: b = int(get_average(a)) results.append([b]) return results = When I pass get_class_average(alice) I receive a zero value for results, which doesnt quite make sense to me. please explain how I'm getting this wrong. Regards ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working collaboratively
On 20/10/15 07:33, Alex Kleider wrote: The output of pydoc for Path.samefile currently reads pathlib.Path.samefile = samefile(self, other_path) Return whether `other_file` is the same or not as this file. pathlib.Path.samefile = samefile(self, other_path) Return whether `other_path` is the same or not as this file. Look closely at what the return value is called in each case. And see how it compares to the names in the signature. -- 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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor