Re: [Tutor] Tutor Digest, Vol 61, Issue 32

2009-03-09 Thread Alan Gauld

"WM."  wrote


From: "Alan Gauld" 



Pyton's error message told you exactly what you had done wrong
although you may not have quite understood it. But do you now see
what the error message was telling you when it said: ...
Can you understand it clearly enough that when a similar error
comes up in future you will know what to look for and where?


No, Mr. G., I cannot.


OK, Let's revisit it to try and unravel what it is saying.


   File "C:\Python26\TicTacToeD.py", line 150, in main
 DisplayBoard(board)
   File "C:\Python26\TicTacToeD.py", line 68, in DisplayBoard
 print "\n\t", board[1], "|", board[2], "|", board[3]
TypeError: 'function' object is unsubscriptable


Start at the bottom and work upwards.


TypeError: 'function' object is unsubscriptable


unsubscriptable means you can't use the [] indexing syntax,
it is not supported by the object to which you have applied it
And the object to which you applied it is a function.


   File "C:\Python26\TicTacToeD.py", line 68, in DisplayBoard
 print "\n\t", board[1], "|", board[2], "|", board[3]


The thing you subscripted was board.
Therefore board must be a function, not something that
you can subscript.


   File "C:\Python26\TicTacToeD.py", line 150, in main
 DisplayBoard(board)


This is where you call DisplayBoard and pass in board
as an argument to DisplayBoard. So you need to look
at the code in TicTacToeD.py to see how board comes
to be a function. There are really only two possibilities:
a) Is it a function to begin with?
ie do you have code that looks like

def board(...):

or

b) Did you assign board to a function?
ie is there a line like

board =

In your case it was 'b', an assignment.

Now at this stage we have reached the end of what the Python
error message is telling us. We need to do some actual debugging
to find out what we assigned to board. Was it a function?
Should it have been?

And here experience comes into play, and we might either guess
that we intended to call the function rather than assign it - ie we
need the parens at the end. OR we check the source code we
are copying to see whether we made a mistake in typing it in.
(Which was the case here)

If that had not yielded any joy we would need to get into deeper
debugging and trace back the source of whatever we assigned
to board. Maybe using print type() to check at what stage
our errant value became a function. But in this case it was
not necessary. The faulty line was

   board = NewBoard<<<--This line

And NewBoard we could tell (because it was defined as such
in our code) was a function, so we almost certainly should have
added parens.

board = NewBoard()

80% of this was deductable from the Python error message
once you get used to reading them carefully and with some
experience. That is why it's important not just to fix the problems
but to understand what Python was telling you for future reference.

If you look back the thread John Fouhy actually told you that
you probably missed some parens within 2 hours of your
original post: He just got the location wrong...

---
something that doesn't support them.  It's telling you that 
'board' is

a function (as opposed to a list or a dict, for example) and functions
don't support [].

Possibly you're meant to call board:

   print "\n\t", board(1), "|", board(2), "|", board(3)

Or, alternatively, you may have assigned to it by mistake somewhere.
---

But it took Brett to point out where you needed to apply the parens!
Which he did by locating the original source code you had copied.

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 61, Issue 32

2009-03-09 Thread WM.

tutor-requ...@python.org wrote:

Send Tutor mailing list submissions to
tutor@python.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
tutor-requ...@python.org

You can reach the person managing the list at
tutor-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

   1. Re: probelm pyhton shell doesnt open help please (Martin Walsh)
   2. Re: while loop problem (Lie Ryan)
   3. problem with an anagram program (jessica cruz)
   4. Re: UNSUBSCRIPTABLE? (Alan Gauld)
   5. Re: problem with an anagram program (Andre Engels)


--

Message: 1
Date: Sun, 08 Mar 2009 23:18:10 -0500
From: Martin Walsh 
Subject: Re: [Tutor] probelm pyhton shell doesnt open help please
To: tutor@python.org
Message-ID: <49b49882.2070...@mwalsh.org>
Content-Type: text/plain; charset=ISO-8859-1

mustafa akkoc wrote:
it gives this message socket error 



IDLE's subprocess didn't make a connection. Either IDLE can't start a
subprocess or personal firewall software is blocking the connection.


IIRC, this was once a known issue with IDLE when combined with the
windows firewall service, or when running multiple instances of IDLE
(perhaps inadvertently). But, I'm having difficulty tracking down the
pertinent bug report(s) -- maybe these have been fixed? or I'm just
tired, probably the latter.

A couple of suggestions from memory ...
1. Check the process/task list for errant pythonw.exe processes, and end
them.
2. Launch IDLE with the -n flag from a terminal (command prompt).
3. Report back to the list with your results, and include the python and
windows version info if you continue to have trouble.

HTH,
Marty

PS. Keep in mind, some of us won't see images you post to the list so
you should generally include a text version of error messages whenever
possible. Or at least, note when you've included an image so that those
with sufficient interest can make the extra effort to view it, if
necessary.




--

Message: 2
Date: Mon, 09 Mar 2009 16:51:28 +1100
From: Lie Ryan 
Subject: Re: [Tutor] while loop problem
To: tutor@python.org
Message-ID: 
Content-Type: text/plain; charset=UTF-8; format=flowed

mustafa akkoc wrote:
hello i have syntax problem in this program 




After correcting the indentations, and putting a missing parentheses in 
line 24 (the last line), I don't see any Syntax Error.


#!/usr/bin/python
# Filename: while.py
number = 23
running = True

while running :
 guess= int(input('Enter an integer : '))  # it give a syntax error 
this line can you me ?


 if guess == number:
 print('Congratulations, you guessed it.')
 running = False # this causes the while loop to stop

 elif guess < number:
 print('No, it is a little higher than that.')

 else:
 print('No, it is a little lower than that.')

else:
 print('The while loop is over.')

 # Do anything else you want to do here
print('Done')  # you missed a paren, probably CopyPasteError



--

Message: 3
Date: Mon, 9 Mar 2009 01:28:38 -0700 (PDT)
From: jessica cruz 
Subject: [Tutor] problem with an anagram program
To: Tutor@python.org
Message-ID: <823438.88941...@web43139.mail.sp1.yahoo.com>
Content-Type: text/plain; charset="iso-8859-1"

I just started learning python an I'm currently working on this program. The purpose of this program is to read a string of letters from user input and print out all the words which are anagrams of the input string. This is what I have and when I try to run the program it says that there is an error "invalid syntax" but I can't figure out where. 





#this reads all of the words in the file into a list
infile = open('/afs/cats/courses/cmps012a-cm/pa1/wordList.txt')
wdcount = int(infile.readline()) #first item is count of all the words
word_list = infile.readlines()
wordList = []

# code that will be compared will be a histogram type code with frequency
# characters
def code(w):
??? hist = []
??? chars = list(w) 
??? chars.sort() 
??? for letter in chars: 
??? if not letter in hist:? # when the letter is not already in hist, 
??? hist.extend([letter, str(w.count(letter))])? # its added to hist along with its freq.
??? else: 
??? continue

??? coding = "".join(hist) # then they are joined as one string
??? return coding




# new list is made with words in word_list followed by its code
for word in? word_list:
??? wordList.append(word) 
??? wordList.append(code(word[:(len(word)-2)])) 



while True:
??? word1 = raw_input('Enter word:') 
??? word = word1.lower() 
??? sig = code(word) 
??? i = 1 
??? if sig in wordList: 
??? print "Anagrams:"
??? while i <= len(wordList):? # when the sig of the inputed wor