Hi all,

This is a problem that has been holding me up for a while in Python. I frequently get issues relating to "global ___ is not defined" and cannot figure out what I'm doing wrong. Here is a coin-flip function I wrote:

def coinflip():	
	import random
	import time

	heads = 0
	tails = 0
	flips = 0

	rec = open('coin toss.txt','w')

	rec.write('Coin Toss Record')
	rec.write('\n')

	N = int(raw_input('Welcome to the Coin Toss!  How many times do you want to flip the coin?: '))

	while (flips < N):
		flip = random.randint(1,2)
		if flip == 1:
			heads += 1
			if heads  > 0:
				rec.write('H ')
				time.sleep(.1)
			flips += 1
		else:
			tails += 1
			if tails > 0:
				rec.write('T ')
				time.sleep(.1)
			flips += 1
	print "There were ", tails ," and ", heads ," heads."

and the script I run the function in is:

import coinflip
import random

coinflip.coinflip()

When I run the script, it will ask for the number of flips, and then I get the error "global name 'random' is not defined." There is nothing else named random in the folder for the file to get confused about, and random works in other settings, just not when I try to use it when using a function in a script. I'm sure this is an obvious fix to those with more experience, but any help would be deeply, deeply appreciated.

Did you save one of your own files named 'random.py' in the directory your coinflip test file is in? Python will look first in the working directory, and a common mistake for beginners is to give their files the name of Python modules.

Did you save one of your own files named 'random.py' in the directory your coinflip test file is in? Python will look first in the working directory, and a common mistake for beginners is to give their files the name of Python modules.

There is no 'random.py' in the working directory.

At the end of your coinflip program insert this ...

# used to test coinflip as a  module
if __name__ == '__main__':
    coinflip()

This allows you to run coinflip as a standalone program. See if you get any errors.

At the end of your coinflip program insert this ...

# used to test coinflip as a  module
if __name__ == '__main__':
    coinflip()

This allows you to run coinflip as a standalone program. See if you get any errors.

I'm not sure what you mean by program, but I tried putting this at the end of both the function and the script and still got the same "global name 'random' is not defined."

I mean something like this ...

import random
import time

def coinflip():

    heads = 0
    tails = 0
    flips = 0

    rec = open('coin toss.txt','w')

    rec.write('Coin Toss Record')
    rec.write('\n')

    N = int(raw_input('Welcome to the Coin Toss!  How many times do you want to flip the coin?: '))

    while (flips < N):
        flip = random.randint(1,2)
        if flip == 1:
            heads += 1
            if heads  > 0:
                rec.write('H ')
                time.sleep(.1)
            flips += 1
        else:
            tails += 1
            if tails > 0:
                rec.write('T ')
                time.sleep(.1)
            flips += 1
    print "There were ", tails ," and ", heads ," heads."

# used to test coinflip as a  module
if __name__ == '__main__':
    coinflip()

... now run coinflip.py without your test program.

Yeah, that did not help, I still got the error on random not being defined.

Clarification: I can run the function itself successfully, but cannot import the coinflip function into a script and have it work-- it tells me 'random is not defined.

This is a truly puzzling problem. Perhaps you'd like to attach your coinflip.py script?

EDIT; The script in vegaseat's very last post works fine for me on python 2.6. Maybe your python installation is incorrectly configured, or...?

EDIT 2: Having read you last post I went back and revised. I used the following as my test.py and it worked fine:

import coinflip
import random  #This line is not needed

coinflip.coinflip()

You would get -->
NameError: global name 'random' is not defined
if you didn't import module random, or if Python cannot find it!

Check your Python directory 'Lib' for file 'random.py'
You can also run this short program ...

# list all the modules Python currently knows about ...
help("modules")

... and see if 'random' is in there.

Clarification: I can run the function itself successfully, but cannot import the coinflip function into a script and have it work-- it tells me 'random is not defined.

what if you just do:

test1.py

import random

print random

test2.py

import test1
This article has been dead for over six months. Start a new discussion instead.