name error global name 'x' is not defined...help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny James

    name error global name 'x' is not defined...help

    Code:
    import subprocess
    import sys
    import time
    import os
    
    def main():
    	#Check to see if quickdraw is located in the correct palce
    	quickfile = file_existance()
    	quickdraw = subprocess.Popen(['java', '-jar', quickfile], stdin = subprocess.PIPE)
    	#Intro, explaining what will occur throughout program
    	intro()
    	#
    	x_coordinate(1)
    	y_coordinate(1)
    	initial_ball(quickdraw)
    
    
    def file_existance():
            filename = raw_input("Please enter the name of the file: ")
            while (not os.path.isfile(filename)):
                    print "That file does not exist, try again"
                    filename = raw_input("Please enter the name of the file: ")
            print os.path.abspath(filename)
            return filename
    
    def intro():
    	print "For this assignment you will be asked to give coordinates for a ball,"
    	print "after you have given the coordinates, the ball will continually bounce"
    	print "until it runs out of energy and comes to a complete stop."
    
    def x_coordinate(num):
    	num= num+1
    	x= int(input("Please enter the x-coordinate (between 0 and 600) where you would like the ball placed: "))
    	if (x <= 600) and (x >= 0):
    		print "Valid choice"
    		return x
    	else:
    		print "invalid choice, choose again please"
    		if num > 3:
    			x = 300
    			return x
    		else:
    			x_coordinate(num)
    	return x
    
    def y_coordinate(num):
    	num= num+1
    	y= int(input("Please enter the y-coordinate (between 0 and 600) where you would like the ball placed: "))
    	if (y >= 0) and (y <= 600):
    		print "Valid choice"
    		return y
    	else:
    		print "invalid choice, choose again please"
    		if num > 3:
    			y = 300
    			return y
    		else:
    			y_coordinate(num)	
    	
    
    
    def initial_ball(quickdraw):
    	radius= 300
    	ball = "fillcircle" + ' ' + str(x) + ' ' + str(y) + ' ' + str(radius)
    	quickdraw.stdin.write(ball)
    	quickdraw.stdin.write("\n")
    The error is that the global name 'x' is not defined. But I dont understand why because I returned the variable...
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You should assign x_coordinate(1) to an identifier and pass that as an additional parameter to initial_ball(). You must do the same for y_coordinate(1).

    You could also do this:
    Code:
        initial_ball(quickdraw,
                     x_coordinate(1),
                     y_coordinate(1))
    Redefine initial_ball: def initial_ball(qu ickdraw, x, y):

    Comment

    Working...