[Tutor] List issues

2014-04-17 Thread Wheeler, Gabriel
Hi

Im having trouble completing this function with lists. Im supposed to create a 
function that will let me know if there are repeating elements so I wrote this 
and am not sure where the error lies. It is supposed to count the number of 
times a number appears and if its greater than 1 then it will say True.


#Problem 3

list = [1,2,2,2,3,4]

def duplicate(list):

for i in range(len[list]):

if list.count(i)  1:

return True


print duplicate(list)



Gabe Wheeler
(512)964-5421
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List issues

2014-04-17 Thread Peter Otten
Wheeler, Gabriel wrote:

 Im having trouble completing this function with lists. Im supposed to
 create a function that will let me know if there are repeating elements so
 I wrote this and am not sure where the error lies. 

It helps you (and us) a lot if you clearly state the error you are seeing. 
If your script bails out with an error post the complete traceback. If all 
appears to be working, but you get a wrong or unexpected result say what you 
get and what you expected. 

Running your code I get

$ cat check_dupes.py 
list = [1,2,2,2,3,4]

def duplicate(list):
for i in range(len[list]):
if list.count(i)  1:
return True

print duplicate(list)
$ python check_dupes.py 
Traceback (most recent call last):
  File check_dupes.py, line 8, in module
print duplicate(list)
  File check_dupes.py, line 4, in duplicate
for i in range(len[list]):
TypeError: 'builtin_function_or_method' object has no attribute 
'__getitem__'

Looking at the line shown in the traceback

for i in range(len[list]):

what could be the function you are not calling but asking for that strange 
__getitem__ attribute? Hint:

 def hello(name):
... print Hello,, name
... 
 hello(Gabriel)
Hello, Gabriel
 hello[Gabriel]
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: 'function' object has no attribute '__getitem__'

That sure looks similar to your error message.

Once you have fixed that I recommend that you add a print statement

def duplicate(list):
for i in range(len[list]): # must be fixed
print looking for, i
if list.count(i)  1:
return True

You'll see that you are looking for items that are not in the list? Can you 
figure out why?

If not, call the function with another list

duplicate([a, b, b, b, c, d])

 It is supposed to count
 the number of times a number appears and if its greater than 1 then it
 will say True.
 
 
 #Problem 3
 
 list = [1,2,2,2,3,4]
 
 def duplicate(list):
 for i in range(len[list]):
 if list.count(i)  1:
 return True
 
 
 print duplicate(list)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List issues

2014-04-17 Thread Dave Angel
Wheeler, Gabriel wheel...@seattleu.edu Wrote in message:


(not much I could read there. This is a text mailing list, so
 please tell your mail program to send in text mode, not html.
 Only parts of your code were visible here, and your question not
 at all. Fortunately, Peter quoted all or most of your message. 
 His comments are all good.  Mine are in addition,  not instead.
 

Your code:

list = [1,2,2,2,3,4]

def duplicate(list):
   for i in range(len[list]):
       if list.count(i)  1:
           return True

print duplicate(list)



Peter's hints should fix your main bugs. Then:

When an if-test doesn't seem to be doing what you expect,  add a
 print statement right before it of the exact expression it's
 testing.  Or even make a new variable of the expression so you
 can be sure you're looking at the same thing. 

When a standard method seems to be misbehaving,  look up its
 definition.  What should the argument to count be?

list is a typename in the standard library,  so it really
 shouldn't be used to name your own objects. I'd use something
 like mylist.

Whenever you see a loop like 
   for i in range(len[list]):

be suspicious of it. Usually you want the items, not the indices. 
for item in mylist:

This function doesn't return any value if the if test always
 fails. Is that what you wanted? 




-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List issues

2014-04-17 Thread Albert-Jan Roskam
 

- Original Message -
 From: Dave Angel da...@davea.name
 To: tutor@python.org
 Cc: 
 Sent: Thursday, April 17, 2014 12:03 PM
 Subject: Re: [Tutor] List issues
 
 quot;Wheeler, Gabriel wheel...@seattleu.edu Wrote in message:
 
 
 (not much I could read there. This is a text mailing list, so
 please tell your mail program to send in text mode, not html.
 Only parts of your code were visible here, and your question not
 at all. Fortunately, Peter quoted all or most of your message. 
 His comments are all good.  Mine are in addition,  not instead.
 
 
 Your code:
 
 list = [1,2,2,2,3,4]
 
 def duplicate(list):
    for i in range(len[list]):
        if list.count(i)  1:
            return True
 
 print duplicate(list)
 
 
 
 Peter's hints should fix your main bugs. Then:
 
 When an if-test doesn't seem to be doing what you expect,  add a
 print statement right before it of the exact expression it's
 testing.  

and/or use the pdb debugger (still on my own todo list, but I know it is 
useful!): http://pymotw.com/2/pdb/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List issues

2014-04-17 Thread Danny Yoo
Hi Gabriel,

Try lists of non-numbers as your input, and the error should be a
little clearer to see.  You should see the conceptual error you're
making if not everything in your program is numeric.

Try:

words = ['hello', 'world', 'hello']
print(words.count(0))
print(words.count('hello'))

First write down what you'd expect to see from this.  Then execute
this snippet.  Does your expectations match what you see?

---

A main bug in your program is that parts of your program are using
numbers for list indices, and other parts of your program are using
numbers as elements, but there's a little confusion in using one
notion for the other.

---

Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor