Me All
Me All

Reputation: 269

Importing a python module and implementing it

I'm learning Python and I'm practicing by creating a simple game: rock, paper, scissors. I have created two Python modules called random_number.py and ro_pa_sc.py. The first one includes the code to generate a random string and the second one implements the game itself.

This is random_number.py:

def r_p_s():
    import random
    random.choice([1, 2, 3])
    if random.choice([1, 2, 3]):
         return "r"
    if random.choice([1, 2, 3]):
         return "p"
    if random.choice([1, 2, 3]):
         return "s"

And this is ro_pa_sc.py:

#!/usr/bin/python
import random_number
def game():
    random_number.r_p_s()
    print r_p_s()
 if __name__ == "__main__":
    game()

However, when I try to run ro_pa_sc.py from the command line in Bash, I get the following error messages:

Error 1: File "ro_pa_sc.py", in module "game()"

Error 2: File "ro_pa_sc.py", in game print r_p_s()

I don't know what the errors could be, since I have checked I imported the modules and I made sure the syntax was correct... It seems that the random_number module might be the problem, but again, I don't know why.

Could anyone tell where my errors are?

Upvotes: 0

Views: 75

Answers (2)

mrsamkhar
mrsamkhar

Reputation: 77

Your r_p_s() function will always return r, because it does not compare the random number to [1, 2, 3], it just chooses from the list and returns r. Try this:

random_number.py:

def r_p_s():
    import random
    rand = random.choice([1, 2, 3])
    if rand == 1:
        return "r"
    if rand == 2:
        return "p"
    if rand == 3:
        return "s"

ro_pa_sc.py:

import random_number
def game():
    print (random_number.r_p_s())
if __name__ == "__main__":
    game()

Upvotes: 1

Vineeth Sai
Vineeth Sai

Reputation: 3447

There are couple of errors in your files.

First, you have the if __name__ == "__main__": indented incorrectly. Notice the extra space in front of the if statement, remove that.

Second, You imported your random_number module into namespace but not r_p_s() directly. So when you're calling the r_p_s() directly in print function it's returning an error. So to fix that you can either change your print function in ro_pa_sc.py to print random_number.r_p_s() or add from random_number import r_p_s that should fix all your problems.

import random_number
def game():
    random_number.r_p_s()
    print random_number.r_p_s() # fix here
if __name__ == "__main__": # fix here
    game()

Note: When you're importing a module using the import <module> statement you can only access the functions within the module using the module's namespace. So all functions must have <module>.function(). To avoid adding the namespace in front of every function, you can use the from statement. from <module> import * where * means all functions in the module. But you must be careful not to cause namespace clashes when many modules share the same function names.

Upvotes: 1

Related Questions