nameerror

NameError: name 'KEYDOWN' is not defined

▼魔方 西西 提交于 2019-12-21 22:21:17
问题 I am new to programming with PyGame and I've made this code: import sys, pygame pygame.init() size = width, height = 800, 600 speed = [2, 2] black = 1, 1, 1 screen = pygame.display.set_mode(size) ball = pygame.image.load("ball.bmp") ballrect = ball.get_rect() player1 = pygame.image.load("player1.png") player1rect = player1.get_rect() mod_x = mod_y = 0 while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == KEYDOWN: if event.key == K_W: movex = 2

open ai gym Nameerror

巧了我就是萌 提交于 2019-12-20 19:39:30
问题 I am trying to use the famous 'Gym' module from OpenAI on WSL and executing code on python 3.5.2. When I try to run an environment as explained here, using the code: import gym env = gym.make('CartPole-v0') for i_episode in range(20): observation = env.reset() for t in range(100): env.render() print(observation) action = env.action_space.sample() observation, reward, done, info = env.step(action) if done: print("Episode finished after {} timesteps".format(t+1)) break this happens : Traceback

NameError: global name 'long' is not defined

纵然是瞬间 提交于 2019-12-20 10:21:15
问题 I have a Python version 3.3.0 and I am not sure why it does not let me do long for b and m here... I tried to look up the answers on here and but nothing helped...thanks im getting an error saying NameError: global name 'long' is not defined power = long(b) % long(m) 回答1: In Python 3.x, use int instead of long . From What’s New In Python 3.0, Integers: PEP 237: Essentially, long renamed to int . That is, there is only one built-in integral type, named int ; but it behaves mostly like the old

Why am I getting a NameError when I try to access an attribute in my class?

两盒软妹~` 提交于 2019-12-20 07:43:17
问题 I have this code with a class: class Triangle(object): def __init__(self, side1, side2, side3): self.side1 = side1 self.side2 = side2 self.side3 = side3 def perimeter(self): return "Perimeter = %s" % (side1 + side2 + side3) a = Triangle(3, 4, 5) print(a.perimeter()) Running this code throws an exception: Traceback (most recent call last): File "untitled.py", line 12, in <module> print(a.perimeter()) File "untitled.py", line 9, in perimeter return "Perimeter = %s" % (side1 + side2 + side3)

Python NameError (def) is not defined [duplicate]

泪湿孤枕 提交于 2019-12-19 09:27:04
问题 This question already has answers here : Python call function within class (2 answers) Closed 4 months ago . I'm having trouble with the following Python code: class Methods: def method1(n): #method1 code def method2(N): #some method2 code for number in method1(1): #more method2 code def main(): m = Methods for number in m.method2(4): #conditional code goes here if __name__ == '__main__': main() When I run this code, I get NameError: name 'method1' is not defined. How do I resolve this error?

Why is my python function not defined, when it exists in the same file?

余生颓废 提交于 2019-12-18 08:54:31
问题 I have a simple function, which I shall call myFunction . It takes two parameters, performs some calculations on them, and returns the result. I also have a class, MyClass , which has a constructor that has a header like this: __init__(self, bar, fun=myFunction): When I try to run anything in this class, I get the following error: MyClass def __init__(self, bar, fun=myFunction): NameError: name 'myFunction' is not defined If I remove this class, I can use myFun in the Python Shell, so what's

Python tkinter 8.5 import messagebox

别说谁变了你拦得住时间么 提交于 2019-12-18 07:12:14
问题 The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox , it runs fine from where ever. from tkinter import * from tkinter import ttk root = Tk() mainFrame = ttk.Frame(root) messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame) Why does IDLE not need the explicit import statement but elsewhere it is required? 回答1: the messagebox is a separate

Python tkinter 8.5 import messagebox

一世执手 提交于 2019-12-18 07:12:10
问题 The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox , it runs fine from where ever. from tkinter import * from tkinter import ttk root = Tk() mainFrame = ttk.Frame(root) messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame) Why does IDLE not need the explicit import statement but elsewhere it is required? 回答1: the messagebox is a separate

python className not defined NameError

强颜欢笑 提交于 2019-12-17 21:36:44
问题 I have a class which i need to instantiate in order to call a method that it contains. When I access it from another class it works fine but when i run from terminal it says : File "myClass.py", line 5, in <module> class MyClass: File "myClass.py", line 23, in ToDict td=MyClass() NameError: name 'MyClass' is not defined Pasting the code: class MyClass: def convert(self, fl): xpD = {} # process some stuff return xpD if __name__ == "__main__": source = sys.argv[1] td = MyClass() needed_stuff =

input(): “NameError: name 'n' is not defined” [duplicate]

隐身守侯 提交于 2019-12-17 09:58:48
问题 This question already has answers here : input() error - NameError: name '…' is not defined (12 answers) Closed 3 years ago . Ok, so I'm writing a grade checking code in python and my code is: unit3Done = str(input("Have you done your Unit 3 Controlled Assessment? (Type y or n): ")).lower() if unit3Done == "y": pass elif unit3Done == "n": print "Sorry. You must have done at least one unit to calculate what you need for an A*" else: print "Sorry. That's not a valid answer." When I run it