0

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?

7
  • You are calling the function ro_pa_sc from the random_number module, and it doesn't exist. The function you wrote is r_p_s. I can't check many more because now (I am on the mobile) but it seems to be the error. Commented Oct 1, 2018 at 5:57
  • You also need to have the if __name__ outside game(). Commented Oct 1, 2018 at 5:59
  • @Ivanhercaz what do you mean by "outside game()"? Commented Oct 1, 2018 at 6:03
  • could you please include complete error message? Commented Oct 1, 2018 at 6:09
  • @AnuvratParashar the error messages are complete! I omitted the lines though, but there wasn't any more info. And my doubts have already been solved. Commented Oct 1, 2018 at 6:15

2 Answers 2

1

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.

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

1 Comment

Thanks! It worked. Would you mind explaining why I have to specify random_number before print r_p_s()? I thought it was enough by just doing import random_number at the beginning.
1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.