0

Hey guys, i got another question. say if you want to only write a few lines of code that can be called on at any time the while the script is running, i'm assuming its the pickle function or something similar to batch like the goto function? Thanks

4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by tondeuse34
0

what you saying is you want to use the same bit of code over and over?
sorry if this is not what you are asking
if so then you use a function

def myFunction(param1, param2):
   print param1
   print int(param2)**3

myFunction("Hello", 2)
myFunction("Joe", 3)

Hope this helps


Chris

0

yeah sorta but what if it is more lines of code with indentions for example:

if choice == 1:
                    print "\nReading..."
myfile = open('myfile.txt', 'r').read()
print "\n",myfile
0

For more lines of code/indentations, its the same exact thing, like so.

def MyFunction(x):
    for y in range(0, x):
        print y
    print "===="
0

Indeed, after the def statement everything indented is included in the function for example.

def myfunc():
   #in function
   #still in function
   #when in a function you can use normal indenting
   #as long as you have the first indent as well
#this has ended the function definition since it is not indented.

Hope that helps

Chris

0

It might be helpful to take a look at the "Starting Python" at the top of this forum. There are 5 pages, so don't have to wade through 100's of pages to get you started.

0

So basically all you have to is define your function in this case which is MyFunction and once you declare that, you can call on that function anywheres in the script including input from the user?

def myfunction(myfile):
    print "\nReading..."
    text = open(myfile, 'r').read()
    print "\n",text

thanks

Edited by vegaseat: forgot the code, corrected code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.