[python] the % operator

Started by
25 comments, last by Fruny 18 years, 10 months ago
Quote:Original post by jordi_0071
i just had to typ the first line press enter then the next line and then enter etc
not everthing right away.


That's as it should be. When you type in commands into the interpreter, you only type in one command at a time. Unless you pack them up on a single line, with each statement separated with a semicolon, but that's not the preferred way.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
but how can you make a game then? since you have to run more lines at a time?
Quote:Original post by jordi_0071
but how can you make a game then? since you have to run more lines at a time?


You put them in a .py file and feed that to the interpreter.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
so it cant be run in the shell itself? i mean code it and run it?
If you put your code in a function, then the interpreter will wait until you've defined the function and called it to actually execute your code. But you will still have to call the function yourself.

def foo():    print "Stop!"    s = raw_input("What is your name? ")    print "You may pass,", s# and laterfoo()


In fact, if you put that in a .py file and execute it, it will do what you want.

The interpreter is useful when you want to test things out, as it lets you load up a program and call the functions yourself, modify the variables, etc. But it's probably not the environment you would want your players to interact with. You want them to play your game, not call its functions by hand. [grin]

On the other hand, I do most of my work in the python interpreter itself. I have built a library of routines that support my needs and use the intepreter essentially as an advanced programmable calculator.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by jordi_0071
so it cant be run in the shell itself? i mean code it and run it?


Yes, it can. It's not really for that sort of development though.
Quote:
###@#### ~ $ python
Python 2.3.5 (#1, Apr 27 2005, 22:53:33)
[GCC 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Stop!"
Stop!
>>> s = raw_input( "What is your name? " )
What is your name? Brandon
>>> s
'Brandon'
>>> print "You may pass,",s
You may pass, Brandon
>>>


What you really should do, as Fruny pointed out, is write that in a function somewhere in a .py file and then run that via "python somefile.py" or such.

def main( ):    print "Stop!"    s = raw_input( "What is your name? " )    print "You may pass,",sif __name__ == "__main__":    main( )

Quote:
###@#### ~ $ python somefile.py
Stop!
What is your name? Brandon
You may pass, Brandon

ok
Quote:

Yes, it can. It's not really for that sort of development though.


how?
Quote:Original post by jordi_0071
Quote:

Yes, it can. It's not really for that sort of development though.


how?


Look at my example. If you define a foo function, you can call it later. All the code in the functions will be executed in one pass. Once the function returns, the interpreter will again wait for you to enter a new command.

That's perfectly normal: the interpreter is a Read-Eval-Print Loop. It reads in a statement (which could be a function definition or a function call), evaluates it, prints the results and goes back to reading.

What exactly are you trying to achieve?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
i though it might be possible to run more then one-line-code so i dont have to save everything and run it in dos.

This topic is closed to new replies.

Advertisement