Re: [Tutor] please help me modify this code so that I can utilize raw_input
On 4 October 2016 at 19:11, Alan Gauld via Tutorwrote: >> """Define a function sum() and a function multiply() that sums and >> multiplies (respectively) all the numbers in a list of numbers. For >> example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) >> should return 24.""" > > > I do wish teachers would not ask students to reinvent the wheel. You may be misunderstanding the teacher's objectives here. Sometimes Python is used to introduce the concept of programming more generally rather than specifically to prepare someone for professional programming using Python. I don't know about you but I came to Python after a number of other programming languages. Now that I teach programming I can see that it's important in a first programming introduction to cover nuts and bolts type algorithmic thinking. sum() is a good example of an algorithm that most people are already used to performing on pen and paper making it a good candidate for learning to use loops and functions. > Python already has a perfectly good sum() function. Writing your own sum function helps to understand what actually happens when using the standard sum function. I do the same thing with my students in C teaching them how to make their own e.g. strlen function. I will tell them that strlen exists afterwards but I want them to understand what the null byte is and writing your own (or just looking at a) strlen implementation helps with that. Likewise simply using a function e.g. strcat without understanding its implementation can lead to segfaults so there can be a relevance in understanding the implementation even if you don't intend to reimplement something in your own code. The implications of the implementation of sum are also non-trivial which is why the stdlib contains at least 3 different sum functions. It's great that Python comes with so many functions to do common tasks. However from personal experience teaching Python as a first language can lead to the disadvantage that many students become spoilt programmers. They will learn to assume that there is a function for exactly what they want and all they ever need to do is ask a precise question in a search engine and read the first stackoverflow page. That may be the correct approach for many problems but it isn't always. A programmer should have the ability to implement basic algorithms (e.g. sum) for themselves when necessary. > And both problems become trivial if using functools.reduce() > Different people think in different ways but I reckon most people - and especially most beginners - would find a loop more straightforward than reduce. I doubt that the OP is at a stage where using reduce seems trivial. -- Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] please help me modify this code so that I can utilize raw_input
On 04/10/16 15:04, Richard Koeman wrote: > I would like to modify this code so that instead of me calling the function > with the list as shown [1,2,3,4], the user inputs the list with raw_input. > You don't need to modify your code you just need ton write a function that reads a list from the user. Notice that this can e done by either repeatedly asking the user to input values until they are done or by reading a string containing the values and converting the string to a list of numbers. > """Define a function sum() and a function multiply() that sums and > multiplies (respectively) all the numbers in a list of numbers. For > example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) > should return 24.""" I do wish teachers would not ask students to reinvent the wheel. Python already has a perfectly good sum() function. And both problems become trivial if using functools.reduce() > > def sum(mylist = [], *args): > print mylist > sum_of_number = 0 > for i in mylist: > sum_of_number += i > print "The sum of the digits in your number is %s" %(sum_of_number) > def multiply(mylist = [], *args): > product_of_number = 1 > for i in mylist: > product_of_number = product_of_number * i > print "The product of the digits in your number is %s" > %(product_of_number) > > sum([1,2,3,4]) > multiply([1,2,3,4]) Incidentally, notice that the assignment asks you to *return* the results not print them. This is good practice, keep the printing separate from the calculation. You can then print the result, if you wish, with print( sum([]) ) hth -- 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
[Tutor] please help me modify this code so that I can utilize raw_input
I would like to modify this code so that instead of me calling the function with the list as shown [1,2,3,4], the user inputs the list with raw_input. Thanks in advance """Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.""" def sum(mylist = [], *args): print mylist sum_of_number = 0 for i in mylist: sum_of_number += i print "The sum of the digits in your number is %s" %(sum_of_number) def multiply(mylist = [], *args): product_of_number = 1 for i in mylist: product_of_number = product_of_number * i print "The product of the digits in your number is %s" %(product_of_number) sum([1,2,3,4]) multiply([1,2,3,4]) -- This is a staff email account managed by Simcoe Muskoka Catholic District School Board. This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the sender. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor