By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

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

P: n/a
Expand|Select|Wrap|Line Numbers
  1. import subprocess
  2. import sys
  3. import time
  4. import os
  5.  
  6. def main():
  7.     #Check to see if quickdraw is located in the correct palce
  8.     quickfile = file_existance()
  9.     quickdraw = subprocess.Popen(['java', '-jar', quickfile], stdin = subprocess.PIPE)
  10.     #Intro, explaining what will occur throughout program
  11.     intro()
  12.     #
  13.     x_coordinate(1)
  14.     y_coordinate(1)
  15.     initial_ball(quickdraw)
  16.  
  17.  
  18. def file_existance():
  19.         filename = raw_input("Please enter the name of the file: ")
  20.         while (not os.path.isfile(filename)):
  21.                 print "That file does not exist, try again"
  22.                 filename = raw_input("Please enter the name of the file: ")
  23.         print os.path.abspath(filename)
  24.         return filename
  25.  
  26. def intro():
  27.     print "For this assignment you will be asked to give coordinates for a ball,"
  28.     print "after you have given the coordinates, the ball will continually bounce"
  29.     print "until it runs out of energy and comes to a complete stop."
  30.  
  31. def x_coordinate(num):
  32.     num= num+1
  33.     x= int(input("Please enter the x-coordinate (between 0 and 600) where you would like the ball placed: "))
  34.     if (x <= 600) and (x >= 0):
  35.         print "Valid choice"
  36.         return x
  37.     else:
  38.         print "invalid choice, choose again please"
  39.         if num > 3:
  40.             x = 300
  41.             return x
  42.         else:
  43.             x_coordinate(num)
  44.     return x
  45.  
  46. def y_coordinate(num):
  47.     num= num+1
  48.     y= int(input("Please enter the y-coordinate (between 0 and 600) where you would like the ball placed: "))
  49.     if (y >= 0) and (y <= 600):
  50.         print "Valid choice"
  51.         return y
  52.     else:
  53.         print "invalid choice, choose again please"
  54.         if num > 3:
  55.             y = 300
  56.             return y
  57.         else:
  58.             y_coordinate(num)    
  59.  
  60.  
  61.  
  62. def initial_ball(quickdraw):
  63.     radius= 300
  64.     ball = "fillcircle" + ' ' + str(x) + ' ' + str(y) + ' ' + str(radius)
  65.     quickdraw.stdin.write(ball)
  66.     quickdraw.stdin.write("\n") 
  67.  
The error is that the global name 'x' is not defined. But I dont understand why because I returned the variable...
Nov 8 '10 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1.     initial_ball(quickdraw,
  2.                  x_coordinate(1),
  3.                  y_coordinate(1))
Redefine initial_ball: def initial_ball(quickdraw, x, y):
Nov 8 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.