[Tutor] IDLE
A while back I made a big fuss about how IDLE indenting works. Kent was finally able to use language simple enough for me to understand. So I kept working IDLE. Today I got an error message. Somebody fixed it! It now indents just like all the other Python windows. Great going, Snake. And thank you guys. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] IDLE
Allen G.asked me how I made IDLE work. I did nothing on purpose. One can open Python into the IDLE window or into the program window. I recently changed from the program window to the IDLE window. IDLE now works exactly right. As a bonus my DOS-oidle window which used to produce error messages now works like a Trojan, also. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] IDLE
At one time I could not run a skit in IDLE nor with the command line. IDLE gave error messages because (Alan G. says) of the 3 chevrons. The command opened into a wee square & issued only error messages. Then the command line opened into a 4x7 box and worked perfectly. IDLE also now works perfectly, whether I open into IDLE or edit window. Below is a copy/paste: IDLE 2.6 >>> i = 9 >>> while i > 2: print i i -= 1 9 8 7 6 5 4 3 >>> just so, with no adjustments ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] UNSUBSCRIPTABLE?
Traceback (most recent call last): File "C:\Python26\TicTacToeD.py", line 165, in main() 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 I am fooling around with Dawson's "...for the Absolute Beginner". The tic-tac-toe program will not run for me. I'm guessing a typi somewhere, but cannot find it. My questions here are; can somebody tell me, in a general way, what the error message means? is there, somewhere, a list of error messages with explanations? is there, somewhere, a dictionary of Python words, such as 'unsubscriptable'? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] UNSUBSCRIPTABLE?
Brett Wilkins wrote: Given that board is a function then I believe that it is likely you're either (a) doing something wrong beforehand, or (b) if board is a function that generates the Tic Tac Toe board, then the syntax you more likely need is board()[number] , but I can't be completely certain. when I say doing something wrong beforehand, I'm thinking you might be accidentally giving the funtion a new name, instead of assigning it's return value to a variable, like so: newname = functionname //see here, no brackets on the end of the function, this basically tells python that newname is also functionname. variable = functionReturningValue() //and here, the function's return value is obviously being assigned to the variable Disclaimer: My naming scheme sucks, I know this :) Cheers --Brett John Fouhy wrote: 2009/3/9 WM. : File "C:\Python26\TicTacToeD.py", line 68, in DisplayBoard print "\n\t", board[1], "|", board[2], "|", board[3] TypeError: 'function' object is unsubscriptable I am fooling around with Dawson's "...for the Absolute Beginner". The tic-tac-toe program will not run for me. I'm guessing a typi somewhere, but cannot find it. "subscript" means "a thing in square brackets after a name". You've got three subscripts in that line: board[1], board[2], and board[3]. The error means that you're trying to use square brackets after 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. Thank you for your remarks. Too bad they fell into my acres of ignorance. One thing is certain, Dawson used brackets [] not parens (). When I spoke of typi (plural of typo) I meant ; for : or \ for /, not line after line of error. My only alternative now seems to be 'get out the old curry comb' and go letter by letter, between the monitor and the page. Headache time. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] UNSUBSCRIPTABLE?
I am using Python 26 on a Windows XP OK, here are the three lines mentioned following the error message. Traceback (most recent call last): File "C:\Python26\TicTacToeD.py", line 165, in main() 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 line 165 = main() def main(): DisplayInstruct() puter, human = Pieces() turn = X board = NewBoard DisplayBoard(board) line 150 = DisplayBoard(board) line 69 def DisplayBoard(board): """Display board on screen.""" print "\n\t", board[1], "|", board[2], "|", board[3] print "\t", "__" print "\t", board[4], "|", board[5], "|", board[6] print "\t", "__" print "\t", board[7], "|", board[8], "|", board[9], "\n" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] UNSUBSCRIPTABLE?
Well, Mr. Wilkins takes the biscuit. He found where I did not enter a pair of parens.() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 61, Issue 32
'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 word is in the word list, ??? if sig == wordList[i] ??? print wordList[i-1]? # the corresponding words are printed ??? i += 2 # then adds two because codes are every other entry ??? else: ??? print "No anagrams" ??? choice = raw_input("Continue? (yes/no)") ??? if choice == 'y' or choice == 'yes': ??? continue ??? else: ??? break ??? ??? ??? ??? ??? ??? ? ??? ??? ??? -- next part -- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/tutor/attachments/20090309/54781b33/attachment-0001.htm> ------ Message: 4 Date: Mon, 9 Mar 2009 08:39:44 - From: "Alan Gauld" Subject: Re: [Tutor] UNSUBSCRIPTABLE? To: tutor@python.org Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response "WM." wrote Well, Mr. Wilkins takes the biscuit. He found where I did not enter a pair of parens.() But do you understand *why* you got the error and what it was telling you? 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: -- 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 -- 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. My approach has been to key in most of Dawson's programs, to get accustomed to the jargon and to get used to some keys that I didn't know were on the keyboard. My long term memory seems to be pretty shot and the stuff is not soaking in. I have found your tutorial, and some others in the Python B/G file of interest, but not retainable. Two O'Reilly books are worthless to me. Dawson's book is very clear; being half way through it, I should know a great deal more than I do. Oh, well, one should not bring a cap-pistol to a knife fight, nor a leaky brain to an academic task. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Don't understand error messages.
Using Windows XP, Python 2.6 & Al Sweigart's "Invent Your Own...", I keyed in the Hangman Program. So far as I can tell it is totally proofed out. Still, I get an error message which makes no sense to me. Herewith, the program, followed by the error message. # CONSTANTS are all CAPS. # variables AreAllLikeThis import random HANGMANPIX = [''' | | | | | | | | | | ===''', ''' | | | Q | | | | | | | ===''', ''' | | | Q | | | | | | | | | ===''', ''' | | | Q | | | | | | | - | | | | ===''', ''' | | | Q | | | | | | | H- | H | H | | ===''', ''' | | | Q | | | | | | | H-H | H H | H H | | ===''', ''' | | | Q | (| | | )| | | H-H | H H | H H | | ===''', ''' | | | Q | (| |) | )| |( | H-H | H H | H H | | ===''', ''' | | | Q | (| |) | )| |( | oH-H | H H | H H | | ===''', ''' | | | Q | (| |) | )| |( | oH-Ho | H H | H H | | ===''', ''' | | | Q | (| |) | )| |( | oH-Ho | H H | H H | O | ===''', ''' | | | Q | (| |) | )| |( | oH-Ho | H H | H H | O O | ==='''] # line 151, equals line 96 in book. Words = 'ant baboon badger bat bear beaver beetle birdamel cat\ clam cobra cougar coyote crab crane crow deerdog donkey duck\ eagle ferret fish fox frog goat goose hawk iguana jackal koala\ leech lemur lion lizard llama mite mole monkey moose moth mouse\ mule newt otter owl oyster panda parrot pigeon python quail rabbit\ ram rat raven rhino salmon seal shark sheep skunk sloth slug snail\ snake spider squid stork swan tick tiger toad trout turkey turtle\ wasp weasel whale wolf wombat worm zebra'.split() def GetRandomWord(WordList): # This function returns a random string from the passed list of strings. WordIndex = random.randint (0, len(WordList) -1) return WordList [WordIndex] def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord): print HANGMANPIX[len(MissedLetters)] print print 'MissedLetters:', for Letter in MissedLetters: print Letter, print Blanks = '_' * len(SecretWord) for i in range(len(SecretWord)): # 116 Replace Blanks with correctly guessed letters. if SecrettWord[i] in CorrectLetters: Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] for Letters in Blanks:#Show SecretWord w 1 space between letters. print Letter, print def GetGuess(AlreadyGuessed):# 124 Returns only a letter user entered. while True: print "Guess a letter." Guess = raw_input()#ppp Guess = Guess.lower() if len(Guess) != 1: print "Please enter a single letter." elif Guess in AlreadyGuessed: print "You used that letter, choose another." elif Guess not in 'abcdefghijklmnopqrstuvwxyz': print "Please enter a LETTER!" else: return Guess #138 def PlayAgain():#Returns T/F for play/quit. print "Wanna do it again? Yes or NO?" return raw_input().lower().startswith('y') print 'H A N G M A N' #145 MissedLetters = '' CorrectLetters = '' SecretWord = GetRandomWord(Words) GameIsDone = False # Let the user enter a letter. while True:#151 DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, SecretWord) Guess = GetGuess(MissedLetters + CorrectLetters) if Guess in SecretWord: CorrectLetters = CorrectLetters + Guess#book158...onscreen213 # 160 Check if user has won. FoundAllLetters = True for i in range(len,(SecretWord)): if SecretWord[i] not in CorrectLetters: FoundAllLetters = False break if FoundAllLetters: print 'Yes! The secret word is "' + \ SecretWord +'"! You have won!' GameIsDone = True else: MissedLetters = MissedLet
Re: [Tutor] Don't understand error messages.
Kent Johnson wrote: On Wed, Apr 22, 2009 at 2:57 PM, WM. wrote: Using Windows XP, Python 2.6 & Al Sweigart's "Invent Your Own...", I keyed in the Hangman Program. So far as I can tell it is totally proofed out. Still, I get an error message which makes no sense to me. def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord): print HANGMANPIX[len(MissedLetters)] print print 'MissedLetters:', for Letter in MissedLetters: print Letter, print Blanks = '_' * len(SecretWord) for i in range(len(SecretWord)): # 116 Replace Blanks with correctly guessed letters. if SecrettWord[i] in CorrectLetters: Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] for Letters in Blanks:#Show SecretWord w 1 space between letters. print Letter, The above line should be indented more than the previous line. I'm not sure if that is the cause of your error but give it a try and see. Kent The indents were dis-arrayed e-mail, due to a long REM; in the script the indents are as you suggest. Blanks = '_' * len(SecretWord) for i in range(len(SecretWord)): # 116 Replace Blanks with correctly guessed letters. if SecrettWord[i] in CorrectLetters: Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] for Letters in Blanks: print Letter, print ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Don't understand error messages.
Well, Kent was right, it was an indent error, but 'way high in the program. I was so catching commas that I got sloppy at an indent change. sorry guys. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] IF statements
I used to do Basic and enjoyed it. Someone said Python was a vastly better language than Visual Basic, which I considered playing with. So I sought to give it a go but struck a sticking point very early. I am now going through the Python tutorial. All went well until I came to IF. The code below was not written by me. It is a copy/paste job from the tutor. I do not have any idea what is going wrong. IDLE 2.6 >>> >>> >>> x = int(raw_input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print 'Negative changed to zero' ... elif x == 0: ... print 'Zero' ... elif x == 1: ... print 'Single' ... else: ... print 'More' ... More 12 SyntaxError: invalid syntax >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] IF statements-1
TO THIS ORIGINAL POST I GOT SIX REPLIES. WM wrote: > > I used to do Basic and enjoyed it. Someone said Python was a vastly > > better language than Visual Basic, which I considered playing with. So > > I sought to give it a go but struck a sticking point very early. > > I am now going through the Python tutorial. All went well until I came > > to IF. The code below was not written by me. It is a copy/paste job > > from the tutor. I do not have any idea what is going wrong. > > > > IDLE 2.6 >>> >>>> >>>> >>>> >>>> x = int(raw_input("Please enter an integer: ")) > > Please enter an integer: 42 >>>> >>>> if x < 0: > > ... x = 0 > > ... print 'Negative changed to zero' > > ... elif x == 0: > > ... print 'Zero' > > ... elif x == 1: > > ... print 'Single' > > ... else: > > ... print 'More' > > ... > > More > > 12 > > SyntaxError: invalid syntax >>>> >>>> IS THIS THE WAY TO ANSWER? OR SHOULD I DO INDIVIDUAL REPLIES? JOHNSON 1 The copy above is exactly from the book, via COPY & PASTE, from IDLE26 thru More. I keyed in the 12 to generate the error message. JOHNSON 2 All that was running was IDLE from the desktop and the tutor from the Python.Org site. I will not do the down-load right now as I want the tutor to work with the program with no tinkering. JOHNSON 3 I did not understand Nair's reply, so I didn't follow thru there. What is "the entire digest" and how do I not include it? LANE I keyed in "12" & "Enter". The rest is copy/paste. I will key the text into Notepad then run it, as you suggest. GAULD Not so. You can check on the tutor, the code stops at 'More'. It's funny about the dots, they were in the tutor but not in IDLE, although the indents were the same. Then, in the e-mil, there they were. IDLE hides dots? What do I know? NAIR Your post to me starts, "also I believe..." It looks like the front end got truncated. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] IF statment 2
Hey, guys, forgive me; I can't help being a fool. Just now I READ the program and it worked perfectly! 42 is more than 1 so it printed "More". My error was that I imagined there was a loop where no loop could be. Oh, me; oh, my. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] SAVING & RUNNING A PROGRAM:
I wrote this two liner. I wanted to save it and then to bring it back as program. IDLE 2.6 >>> brain = 'life' >>> print brain life I saved it as brain.py, IDLE 2.6 >>> brain = 'life' >>> print brain life >>> python/brain.py Traceback (most recent call last): File "", line 2, in python/brain.py NameError: name 'python' is not defined >>> I think the line which drew the error message is close, but I have tried many versions and nothing works. What am I doing wrong? I get a similar result if I do the command line thing.l ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] RUNNING A PROGRAM
a = "Futzenburgerstein" b = ( 7 + 2 ) / 3 c = b / 2 print a, b, c The above text was copied from a window named "??futz.py-C:\Python26\futz.py" The ?? is two red script characters which I cannot read. When I go 'F5' or Run > Run Module I get kicked back into IDLE. Shouldn't 'F5' get me a window with a, b & c printed all in a row? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] PYTHON ON DOS
Some say that Python programs run better on DOS. I cannot find a way to do that. I can go 'Python Command Line' to wind up on a black screen version of IDLE but I can't get from the interactive to executive mode. Is there any advantage to running .py on DOS? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] IDLE CAN'T FIND SOCKET...
I keep getting the error message & must re-boot to enable IDLE work. Is there some rule about 'saving' or about having too many windows open? It seems to happen if I am doing a lot of saving & re-saving. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Running Python from REVO screen???
I asked tutor how to get to the black screen to run Python programs. I got a three step answer but could not get step 1. to work; 1. XP: Start > Run > type "cmd" > OK C:\Documents and Settings The above line is where I wind up & I cannot get out of it. I can get into it with Start > Programs > Accessories > Command Prompt. The below lines I have not yet gotten to. What is 'cd'? 2. then cd to the folder where you put the script: C:\> cd C:\mypython\ 'cd'? 3. then run the program by typing "python blah.py" C:\mypython\> python blah.py ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 58, Issue 2
Stooges.py i,j,k = 3,3,3 while i != 1: print 'Larry, Moe & Curly Joe!' i -= 1 while j != 1: print 'Go Mad!!' j -= 1 while k != 1: print 'Go-go bad-bad!!' k -= 1 print '\nBye-bye.' I am trying to learn loops. These nested 'whiles' work OK but I would like to wrap this script in a 'for' loop. I have not been able to make that work. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] 'for' loops
I recently asked a question about 'for' loops, expecting them to be similar to 'for-next' loops. I have looked at several on-line tutors but am still in the dark about what 'for' loops do. Does anyone have a plain English about the use of 'for' loops? Are 'while' loops the only way Python runs a sub-routine over & over? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] I asked about loops
and your response was most gratifying. I think that I now have a handle on the subject and want to say, "Thanks to you all." WM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Wayne's waning list.
As a BASIC, hobby programmer, (long since), I get so jargonized here that I seldom ask about anything any more. The only useful bit I have gleaned from reading many, many posts is the URL for projecteuler. Maybe, after I get past the baby steps and start using the libraries, I will benefit more. Now, the opaque questions and bewildering responses leave me in a very passive state. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WAYNE'S WAINING...
One of the replies to my post explained how difficult it is to address an audience composed of several levels of skill. I understand that, nor was I condemning anyone who has a better command of jargon than I have. Jargon is essential to any trade. What I wanted to do was give Wayne the POV of someone who posted for a while, then gave up. I have plenty of questions, I shall try to put some of them in intelligible form, for your amusement. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Interactive programming.
>>> i = 5 >>> j = 7 >>> if i <= j: print 'nudge, nudge' else: File "", line 3 else: ^ IndentationError: unexpected indent Running in IDLE, all is well until "else:". IDLE seems perplexed about the >>>s. I try to de-dent else via the backspace key, after that there is no way to avoid an error message. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive programming.
Norman Khine wrote: >>> i = 5 >>> j = 7 >>> if i <= j: ... print 'nudge', 'nudge' ... else: ... print 'whatever' ... nudge nudge >>> WM. wrote: >>> i = 5 >>> j = 7 >>> if i <= j: print 'nudge, nudge' else: File "", line 3 else: ^ IndentationError: unexpected indent Running in IDLE, all is well until "else:". IDLE seems perplexed about the >>>s. I try to de-dent else via the backspace key, after that there is no way to avoid an error message. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Yes, I understand how your program works. What I do not understand is how you got it. My program came out in IDLE as you see it. No ..., different indentation, an error message before I could add the else alternative. (Which, as a Pythonista, one should know, is "Wink-wink".) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive programming.
IDLE 2.6 >>> i = 1 >>> j = 11 >>> if j > 1: print j else: SyntaxError: invalid syntax >>> I am getting a little dizzy here. I know about text editor, code, save, F5. Many tutorials say that it is funner and faster to test an idea 'interactively', using IDLE. Nothing is said about fiddling the output. In the above bit the 'else' is in the wrong place because IDLE or PYTHON terminated the script. I hate to be a bore here, but if the mechanics of the program are not as they are said to be in the tutorials, how am I to proceed? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive programming.
Norman Khine wrote: >>> i = 5 >>> j = 7 >>> if i <= j: ... print 'nudge', 'nudge' ... else: ... print 'whatever' ... nudge nudge The above is just what the tutorials said would happen. Can anyone give me a step-by-step in IDLE 2.6 that would make this happen? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive programming.
IDLE 2.6 >>> i = 1 >>> if i > 1: print 'x' else: print 'y' y >>> Last post on this topic, I guess. I think that the script looks pretty lame, though. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Thank you, Alan.
I felt such a monkey until Kent convinced me that the 'else' only appeared to be un-indented. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] HOW DO I PYTHONIZE A BASICALLY BASIC PROGRAM?
# The natural numbers(natnum), under 1000, divisible by 3 or by 5 are to be added together. natnum = 0 num3 = 0 num5 = 0 cume = 0 # The 'and' is the 15 filter; the 'or' is the 3 or 5 filter. while natnum <= 999: num3 = natnum/3 num5 = natnum/5 if natnum - (num3 * 3) == 0 and natnum - (num5 * 5) == 0: cume = cume + natnum elif natnum - (num3 * 3) == 0 or natnum - (num5 * 5) == 0: if natnum - (num3 * 3) == 0: cume = cume + natnum elif natnum - (num5 * 5) == 0: cume = cume + natnum natnum = natnum + 1 print cume This problem was kicked around last month and I did not understand any of the scripts. So I tried to recall the BASIC ifs and loops. The project euler guys say it works, but how might it be made more Pythonic? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] newton's sqrt formula
# program to find square root square = input ('Please enter a number to be rooted, ') square = square * 1.0 guess = input('Please guess at the root, ') guess = guess * 1.0 newguess = 0. while guess**2 != square: # Newton's formula newguess = guess - (guess * guess - square) / (guess * 2) guess = newguess guess**2 - square print print print guess, ' is the square root of ', square print print print 'bye' Last month there was a square root program discussed. I wondered if the tide of my ignorance had receded enough that I could take a whack at messing with it. I offer this rewrite for your critique. Can it be terser, faster, prettier? Thank you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] newton's square root formula
# program to find square root square = float(raw_input ("Please enter a number to be rooted, ")) guess = input("Please guess at the root, ") i = 0 while guess**2 != square: i+=1 # Newton's formula guess = guess - (guess * guess - square) / (guess * 2) print i print "\n\n\n%s is the square root of %s" % (guess, square) print "\n%s loops were run." % (i) print "\n\n\nbye" # Here is my program, enhanced by Alan's good advice. The reason I wanted to re-write the other program was, it had a limited number of loops and I felt accuracy should be the measure. So, just now, I added a loop counter here and found that the formula is a little buggy. Make 'square = 7' and you will be in the 'i = 500' area before you can find ControlC. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor