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

Recommended Answers

All 6 Replies

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

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

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 "===="

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.