Re: [Tutor] Another Newbie question
That's because you're doing it in interactive mode. In interactive mode, the code is treated like commands, it is executed immediately after the command is finished. You may differentiate Interactive Mode and Normal/Coding Mode by the prompt, in Coding Mode there is no prompt cause, in Interactive mode, there is the '>>>' (default) Example in Interactive Mode: >>> print 'Blah blah blah' Blah blah blah >>> for i in xrange(5): ... print i ... 0 1 2 3 4 >>> Some "commands", like 'for', 'while', dictionary literal, etc may require more than one line, for those, the secondary prompt is shown '...', although that depends on how you start python, if you started python from IDLE, the secondary prompt is not, by default, shown. That's a bit basic. Now to the specific reason why python (interactive mode) "doesn't wait" you to finish your command. In interactive mode, a blank line is considered to be the end of multi-line command, so: >>> for i in xrange(4): ... print i ... # The next line is empty ... 0 1 2 3 >>> that empty line is an instruction to start executing the multi-line commands immediately (or another way to see it, an empty line is considered to be the end of current instruction) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Another Newbie question
On Tue, Jun 24, 2008 at 6:23 AM, Danny Laya <[EMAIL PROTECTED]> wrote: > Hi I got some problem about writting convention in python. Some tutorial ask > me to write this : > > a = 1 > s = 0 > print 'Enter Numbers to add to the sum.' > print 'Enter 0 to quit.' > while a != 0: > print 'Current Sum:', s > a = int(raw_input('Number? ')) > s = s + a > print 'Total Sum =', s > > And the response must be like this : > > Enter Numbers to add to the sum. > Enter 0 to quit. > Current Sum: 0 > Number? 200 > Current Sum: 200 > Number? -15.25 > Current Sum: 184.75 > Number? -151.85 > Current Sum: 32.9 > Number? 10.00 > Current Sum: 42.9 > Number? 0 > Total Sum = 42.9 > > But when I write until this : > a = 1 s = 0 print 'Enter Numbers to add the sum' > Try putting the program in a function. A function is defined using: def functionName(): Everything inside the function is indented. For example: def main(): a = 1 s = 0 print 'Enter Numbers to add to the sum.' print 'Enter 0 to quit.' while a != 0: print 'Current Sum:', s a = int(raw_input('Number? ')) s = s + a print 'Total Sum =', s main() In this example, the function is called main() and it is defined with with the keyword 'def' followed by the name of the function, parentheses, and finally a colon (:). Don't forget the colon! The body of the function is indented. Make sure you indent the lines inside the function when you are entering it in the interactive interpreter. The while loop needs more indentation with the function body! Look at it carefully! Finally, call the function. In the above example, the function is called by entering: main() on a line by itself. It is NOT a part of the function body. I hope this is helpful. I remember when I was first starting out with the Python interactive interpreter. It wasn't easy. Good luck! Stick with it. It won't be long before you look back on these beginning days and laugh. Happy Programming! -- b h a a l u u at g m a i l dot c o m Kid on Bus: What are you gonna do today, Napoleon? Napoleon Dynamite: Whatever I feel like I wanna do. Gosh! > I press enter, and alas my python response me : > Enter Numbers to add the sum > > It didn't want waiting me until I finish writing the rest. > I know there is some mistake about > my writing convention, > but what ??? Can you find it ?? > > But you know it's not finish,I ignore the error message and > writng the rest, but until i write this: > while a != 0: > ... print 'Current Sum:', s > ... a = int(raw_input('Number?')) > ... s = s+a > ... print 'Total Sum =', s > > Oh, man... another error message : > > File "", line 5 > print 'Total Sum =', s > ^ > > Can you help me guys ?? > > > > > > > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Another Newbie question
Le Tuesday 24 June 2008 12:23:41 Danny Laya, vous avez écrit : > Hi I got some problem about writting convention in python. Some tutorial > ask me to write this : > > a = 1 > s = 0 > print 'Enter Numbers to add to the sum.' > print 'Enter 0 to quit.' > while a != 0: > print 'Current Sum:', s > a = int(raw_input('Number? ')) > s = s + a > print 'Total Sum =', s > > And the response must be like this : > > > But when I write until this : > >>> a = 1 > >>> s = 0 > >>> print 'Enter Numbers to add the sum' > > I press enter, and alas my python response me : > Enter Numbers to add the sum This is because you're doing this in an interactive session, so python interprets and runs each line immediately after you write them. I guess this example was supposed to be put in a file and executed from there, but you can also put all this in a function : def test_func() : a = 1 s = 0 print 'Enter numbers to add to the sum.' ... This won't do anything until you call the function: test_func() > > It didn't want waiting me until I finish writing the rest. > I know there is some mistake about my writing convention, > but what ??? Can you find it ?? > > But you know it's not finish,I ignore the error message and > > writng the rest, but until i write this: > >>> while a != 0: > > print 'Current Sum:', s > a = int(raw_input('Number?')) > s = s+a > print 'Total Sum =', s > > Oh, man... another error message : > > File "", line 5 > print 'Total Sum =', s > ^ You just forgot the most important: the error message itself :) Probably an indentation error, but we'll need the full message to help (something like "SomeError: blah blah...") -- Cédric Lucantis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Another newbie question from Nathan.
If I understand your problem correctly all you need to do is use the name of the function to call it ie: def func(): print "hello world" func() would give the output "hello world"On 7/10/05, Nathan Pinno <[EMAIL PROTECTED]> wrote: Hi all, How do I make Python get a def? Is it the "get" function, or somethingelse? I need to know so that I can get a def for that computerMasterMind(tm) game that I'm writing. BTW, I took your advice, and wrote some definitions for my Giant Calculator program. Might make the code easier to read, but harder tocodebecause I have to keep going to the top to read the menu. Not that fun,butnecessary for a smooth program, I guess. Nathan Pinno "Steven D'Aprano" <[EMAIL PROTECTED]> wrote in messagenews:[EMAIL PROTECTED] > On Sat, 02 Jul 2005 00:25:00 -0600, Nathan Pinno wrote: >> Hi all. >> How do I make the computer generate 4 random numbers for theguess? Iwant >> to know because I'm writing a computer program in Python like thegame >> MasterMind. > First you get the computer to generate one random number. Then youdo it > again three more times. > If you only need to do it once, you could do it this way: > import random # you need this at the top of your program > x0 = random.random() > x1 = random.random() > x2 = random.random() > x3 = random.random() > But if you need to do it more than once, best to create a functionthat > returns four random numbers in one go. > def four_random(): >"""Returns a list of four random numbers.""" >L = [] # start with an empty list >for i in range(4): >L.append (random.random()) >return L > and use it this way: > rand_nums = four_random() > # rand_nums is a list of four numbers > print rand_nums[0] # prints the first random number > print rand_nums[3] # prints the last one > or like this: > alpha, beta, gamma, delta = four_random() > # four names for four separate numbers > Steven. > http://mail.python.org/mailman/listinfo/python-list___Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Another newbie question from Nathan.
Hi all, How do I make Python get a def? Is it the "get" function, or something else? I need to know so that I can get a def for that computer MasterMind(tm) game that I'm writing. BTW, I took your advice, and wrote some definitions for my Giant Calculator program. Might make the code easier to read, but harder to code because I have to keep going to the top to read the menu. Not that fun, but necessary for a smooth program, I guess. Nathan Pinno "Steven D'Aprano" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > On Sat, 02 Jul 2005 00:25:00 -0600, Nathan Pinno wrote: >> Hi all. >> How do I make the computer generate 4 random numbers for the guess? I want >> to know because I'm writing a computer program in Python like the game >> MasterMind. > First you get the computer to generate one random number. Then you do it > again three more times. > If you only need to do it once, you could do it this way: > import random # you need this at the top of your program > x0 = random.random() > x1 = random.random() > x2 = random.random() > x3 = random.random() > But if you need to do it more than once, best to create a function that > returns four random numbers in one go. > def four_random(): >"""Returns a list of four random numbers.""" >L = [] # start with an empty list >for i in range(4): >L.append(random.random()) >return L > and use it this way: > rand_nums = four_random() > # rand_nums is a list of four numbers > print rand_nums[0] # prints the first random number > print rand_nums[3] # prints the last one > or like this: > alpha, beta, gamma, delta = four_random() > # four names for four separate numbers > Steven. > http://mail.python.org/mailman/listinfo/python-list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor