Re: [Tutor] help with homework
On Mon, Oct 29, 2012 at 2:28 PM, Alan Gauld wrote: > On 29/10/12 08:37, Asokan Pichai wrote: > >>> teachers put stupid artificial constraints on your code, >> >> >>> >>> such as banning the use of len(). >> >> >> There may be legitimate learning outcomes for a teacher >> to specify such conditions. > > > In that case they should think up a scenario that requires the use of the > construct that is most appropriate. Teachers should never encourage bad > practice(*) and that's what this example does. > > It's valid to put constraints such as "do not use any external > modules" or to use a specific construct if that's what's being taught. > But it's never right to leave the student the freedom to use any solution > *except* the one that is most logical and readily available. > > >> In this case, learning to use a counter that is incremented >> under certain conditions. > > > But there are many better cases where that solution is needed rather than > using len(). This one just sounds like lazy teaching. > > (*) Actually enforcing bad practice one to demonstrate the problems > can be valid provided its followed immediately by the best practice > alternative. As a trainer, I believe using a bad example is WRONG; even to teach how not to write. Better to critique the suggested bad answers and explain why that is bad, rather than enforce a constraint that leads to a bad way and then call it out as bad explain why. That said, it *is* preferable IMO, not use such strong condemnation without knowing full background. Probably by now this is OT, so I should stop now. Asokan Pichai If a language is designed for non-programmers, soon only non-programs get written in it. --- Anonymouse ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with homework
On 29/10/12 08:37, Asokan Pichai wrote: teachers put stupid artificial constraints on your code, such as banning the use of len(). There may be legitimate learning outcomes for a teacher to specify such conditions. In that case they should think up a scenario that requires the use of the construct that is most appropriate. Teachers should never encourage bad practice(*) and that's what this example does. It's valid to put constraints such as "do not use any external modules" or to use a specific construct if that's what's being taught. But it's never right to leave the student the freedom to use any solution *except* the one that is most logical and readily available. In this case, learning to use a counter that is incremented under certain conditions. But there are many better cases where that solution is needed rather than using len(). This one just sounds like lazy teaching. (*) Actually enforcing bad practice one to demonstrate the problems can be valid provided its followed immediately by the best practice alternative. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with homework
[SNIPPED] > > Always use a while loop in this situation, This is excellent advice. Use a for loop in two situations: 1. Iterating a fixed(known) number of times 2. Iterating through the contents of any container Use a while loop to iterate as long as a condition applies. > regardless of whether or not > teachers put stupid artificial constraints on your code, > such as banning the use of len(). There may be legitimate learning outcomes for a teacher to specify such conditions. In this case, learning to use a counter that is incremented under certain conditions. I would hesitate to condemn in such strong terms without knowing more background. Asokan Pichai ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with homework
On 29/10/2012 01:40, Matthew Ngaha wrote: In your original getNames do something like this. Initialise a counter to zero. Every time you get a valid name increment the count. If the count is three you're finished. hey i was looking at the question as im learning also. With the counter, would you use a while loop instead of a for loop, with the condition being something like, while count < 3: ? without len() the for loop option seems difficult Always use a while loop in this situation, regardless of whether or not teachers put stupid artificial constraints on your code, such as banning the use of len(). -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with homework
> > In your original getNames do something like this. >> Initialise a counter to zero. >> Every time you get a valid name increment the count. >> If the count is three you're finished. >> > hey i was looking at the question as im learning also. With the counter, would you use a while loop instead of a for loop, with the condition being something like, while count < 3: ? without len() the for loop option seems difficult ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with homework
On 28/10/12 21:37, Sandra Beleza wrote: def GetNames(): names=[] while len(names)<3: name=raw_input("Name: ") if name in names: print name, "is already in the data. Try again." if name not in names: names.append(name) names.sort() You should probably stop your function here by returning names. for each in names: print "Hurray for", each +"!" print It's good practice to keep the presentation logic outside the function. Your loop would then change to for each in getNames(): ... However I cannot use the len() built in function. The Teacher is asking for another solution that does not use the len() t That's bizarre. It forces you to write a bad solution. However, Mark has already given you the necessary hint. But using len() is absolutely the sane way to do this (unless you are paranoid about micro performance). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with homework
On 28/10/2012 21:37, Sandra Beleza wrote: Hi, I have to write a script that asks the user for names one at a time, and accept the name only if the user did not gave it before. The script has to do this until it gets 3 unique names. So far I have this: def GetNames(): names=[] while len(names)<3: name=raw_input("Name: ") if name in names: print name, "is already in the data. Try again." if name not in names: names.append(name) names.sort() for each in names: print "Hurray for", each +"!" print However I cannot use the len() built in function. The Teacher is asking for another solution that does not use the len() t I know I can do it using the command: def Ask def GetNames(how_many): names=[] for el in range(how_many): name=raw_input("Name: ") if name in names: print name, "is already in the data. Try again." if name not in names: names.append(name) names.sort() for each in names: print "Hurray for", each +"!", print I cannot get 3 names, and it is easy to understand why (because the loop only iterates 3 times). But I don't know how to ask the user for names one at a time and to obtain 3 names and an output that looks like this: Name #1: Lewis Name #2: John Name #3: John John is already in the data. Try again. Name #3: Name #3: Chris Hurray for Chris! Hurray for John! Hurray for Lewis! Many Thanks! In your original getNames do something like this. Initialise a counter to zero. Every time you get a valid name increment the count. If the count is three you're finished. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] help with homework
Hi, I have to write a script that asks the user for names one at a time, and accept the name only if the user did not gave it before. The script has to do this until it gets 3 unique names. So far I have this: def GetNames(): names=[] while len(names)<3: name=raw_input("Name: ") if name in names: print name, "is already in the data. Try again." if name not in names: names.append(name) names.sort() for each in names: print "Hurray for", each +"!" print However I cannot use the len() built in function. The Teacher is asking for another solution that does not use the len() t I know I can do it using the command: def Ask def GetNames(how_many): names=[] for el in range(how_many): name=raw_input("Name: ") if name in names: print name, "is already in the data. Try again." if name not in names: names.append(name) names.sort() for each in names: print "Hurray for", each +"!", print I cannot get 3 names, and it is easy to understand why (because the loop only iterates 3 times). But I don't know how to ask the user for names one at a time and to obtain 3 names and an output that looks like this: Name #1: Lewis Name #2: John Name #3: John John is already in the data. Try again. Name #3: Name #3: Chris Hurray for Chris! Hurray for John! Hurray for Lewis! Many Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor