2

Sorry if this is a silly question I've researched it and found similar code that works and applied the same layout, but for some reason when I run mine it doesn't print num or the true or false result, just a blank line. I'm using Python 2, what am I missing? the rand_divis_3 function should just simply generate a random number and print True or False depending on whether it is divisible by 3. Help much appreciated, I'm new to python

import random
def rand_divis_3():
    num = random.randint(0,100)
    print num
    if num % 3 == 0:
        print True
    else:
        print False
1
  • 2
    Did you called rand_divis_3() function ? Commented Dec 30, 2014 at 13:35

2 Answers 2

3

Python is a scripting language as you know. It works a little bit different from what you might have experienced about C/C++/Java. In C/Cpp/Java you have a main class and or a main function available which is the point of start for the execution of the program.

For python there is no language specified main function or class, you have to define your own class and call it also.

What you had done was just define the function without using it, so call the function.

Snippets:

import random
def rand_divis_3():
    num = random.randint(0,100)
    print num
    if num % 3 == 0:
        print True
    else:
        print False

This creates a function rand_divis_3, this is created and stored in the internal memory. The thing is you have to call the function.

>>>rand_divis_3()
93

Now this part is tricky, python is a language that literally has millions of libraries, you can find them at https://pypi.python.org, also you can create your own libraries.

Suppose you wrote this code

import random
def rand_divis_3():
        num = random.randint(0,100)
        print num
        if num % 3 == 0:
            print True
        else:
            print False
rand_divis_3() #This is where the function is executed

you called this file.py and executed it as python file.py, that will ofcourse first import random, then create an object called rand_divis_3 and internally it points to the function. and when it will come to rand_divis_3(), it will execute this line, which will in effect generate a random number for you and print the number and True/False accordingly.

Suppose you need to create a library, then you have a problem, because when you import file then the file.py script is executed from top to bottom.

To avoid this you can do

  1. replace import file by from file import rand_divis_3,which selectively imports a function, though if you are working on some real project this isn't advisable, look #2
  2. at the end of the script add this line,

    if name=='main': rand_divis_3()

With this line at the end of file.py, when you do python file.py it will call the function but if you do import file, the function rand_divis_3 will not be called.

The reason behind this is when name variable is main when you are executing any python script.

So your final file will look like this:

import random
def rand_divis_3():
        num = random.randint(0,100)
        print num
        if num % 3 == 0:
            print True
        else:
            print False

if __name__=='__main__':
   rand_divis_3() # function is called  only when you execute the script

Note: This is very important, what Cyber said works when you are learning the language to do few things, but when you get serious about programming this will come to handy, it had taken me a long time to realize it's importance.

I have a github repo created for python newbies here, http://github.com/thewhitetulip/SamplePythonScripts

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for taking the time to write this, I will be doing some large group projects later in the year, so i will refer back to this and keep it in mind. Very helpful, thank you!
You are welcome. Actually when I was starting out programming it took me a very long time to really understand this.
3

You need to actually call the function. If you do so, you will notice your function works fine.

>>> rand_divis_3()
26
False

>>> rand_divis_3()
15
True

All you did was define the function for future use.

1 Comment

Ah yes I see, sorry to be annoying! Just started at this, trying to get my head around it! Thanks very much! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.