def is_table(tab):
if len(tab) != 3:
return False
valid = (-1, 0, 1)
for a in range(0, 3):
if len(tab[a]) != 3:
return False
for b in range(0, 3):
if tab[a][b] not in valid:
return False
return True
When I try to run is_table(((0,0,0),(0,0,0),(0,0,0))) on console, I get this error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'is_table' is not defined
Can anyone explain why? My function is clearly defined, but it still doesn't run on console.
The python console dont know about your files functions. First, edit your file name, removing any space, e.g., jogo_do_galo.py (or another name, like jogo_do_mengao).
Open the python console, and try:
>>> from jogo_do_galo import *
>>> tab = ((True, 0, 0), (0, 0, 0), (0, 0, 0))
>>> eh_tabuleiro(tab)
This will work.
Did you define the function inside the console, or in a separate file?
If you defined the function in a separate file, you'll need to run the file before the function will be recognized by the console.
Found an answer. Right-clicked on file name, settings, run on console.
Related
I have a class with a method called delete that should completely erase the "hold_files" variable. I'd like to mimic deleting an actual folder in the terminal, then itself. For example, "rm -f". Is that possible?
Also, how would I prove that hold_files is completely gone?
class directory:
hold_files = [
'test1.txt', 'test2.py',
{'/desktop': ['computer.txt','tfile.doc',
{'/steph':{
'/pictures': [
'hello.gif',
'smile.gif',
'run.gif'
]},
'/work':[
'file1.txt',
'file2.txt'
]
}]
}
]
#recursively delete folder
def delete(itself):
#if dictionary, call self, else delete
if len(itself)>0:
for f in itself:
print(type(f))
if type(f) is dict:
delete(f)
else:
itself.remove(f)
This is calling the method:
directory.delete(hold_files)
Thank you.
You should be able to use del to delete a variable, and then try using the variable to check if it's really gone. If you get an error, it's gone.
`
>>> number = 1
>>> del number
>>> data = number
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
data = number
NameError: name 'number' is not defined
>>>
`
I am trying to make a script that disables cmd. Here is my script:
import winreg
registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\Policies\Microsoft\Windows\System", 0, winreg.KEY_WRITE)
winreg.SetValueEx(registry_key, "DisableCMD", 0, winreg.REG_DWORD, "1")
But I get
Traceback (most recent call last):
File "C:\Users\username\winregg.py", line 3, in <module>
winreg.SetValueEx(registry_key, "DisableCMD", 0, winreg.REG_DWORD, "1")
ValueError: Could not convert the data to the specified type.
What am I doing wrong and how do I fix it?
Try this...
winreg.SetValueEx(registry_key, "DisableCMD", 0, winreg.REG_DWORD, 1)
This the code that has the error:
print("Through 1-10 write a number that is going to represent how far you should throw the ball for " + playerCMD4 + "to catch") ; sleep(float(speed))
playerNumberCMD = raw_input()
import random
def allResponses(arg1):
allResponses = arg1
allResponses = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def randomResponse(arg1):
randomResponse = arg1
randomResponse = random.choice(allResponses)
if randomResponce == playerNumberCMD:
print(playerCMD4 + " caught the ball.") ; sleep(float(speed))
The error I get is this:
Traceback (most recent call last):
File "classref3.py", line 3, in <module>
class Dog:
File "classref3.py", line 50, in Dog
if randomResponce == playerNumberCMD:
NameError: name 'randomResponce' is not defined
Was Defnot the correct way to go or is it something else?
As the error states you have not defined the variable "randomResponce".
If you look at the previous line where you think you have defined the variable, you have defined "randomResponse". Note the different spelling. The spelling needs to be the same.
I would also caution against using the same name for a variable and a function in the same script.
Question: I am having some issues with my old scripts that do not work on Python 3.x
Offtopic; how flexible is Python when trying to access binary and text files for mass renaming, renumbering? within Collision and IMG archives?
I do not have the best understanding of this anymore as I have gone the direction of Level design using 3dsmax.
Anyway..
Error:
Traceback (most recent call last):
File "C:\SOL_REM.py", line 26, in <module>
process_ide(sys.argv[1], int(sys.argv[2]),
File "C:\SOL_REM.py", line 18, in process_ide
ide_line = reduce(lambda x,y: str(x)+","+st
NameError: global name 'reduce' is not defined
Code:
import sys
if len(sys.argv) < 4:
sys.exit('Usage: Source ide | ID number | Dest ide filename.' sys.argv[0])
def process_ide(ide_source, num, ide_destination):
src = open(ide_source,'r')
dst = open(ide_destination,'w')
for line in src:
ide_line = line
if not (line == "" or line[0]=="#" or len(line.split(",")) < 2):
ide_line = line.split(",")
ide_line[-1] = ide_line[-1][:-2]
ide_line[0] = num
num+=1
ide_line = reduce(lambda x,y: str(x)+","+str(y), ide_line)+"\n"
dst.write(ide_line)
src.close()
dst.close()
process_ide(sys.argv[1], int(sys.argv[2]), sys.argv[3])
Starting out simple:
What I am trying to do is parse an ide text file by changing numbers in enumerate order.
Syntax would be SOL_rem.py game.ide 1845 game2.ide
Example file:
ID Modelname TexName Rendering flags.
objs
1700, ap_booth2_03, ap_airstuff1, 1, 190, 0
1701, ap_seaplaland1, ap_seasplane, 1, 299, 0
1702, ap_seaplanehanger1, ap_seasplane, 1, 299, 0
1703, ap_termwindows1, ap_termwindows, 1, 299, 4
1704, ap_blastdef_01, ap_newprops1opac, 1, 299, 4
1705, ap_blastdef_03, ap_newprops1opac, 1, 299, 4
1706, air_brway_030, airgrndb, 1, 299, 0
end
The IDs would be re-adjusted from 1845 in ascending order.
reduce is no longer in the builtin namespace in Python 3.
Instead of using reduce, why not just use a join?
ide_line = ','.join(ide_line) + '\n'
In Python3, you can do
from functools import reduce
And even in Python2.6+ the above should be okay, but is not required.
Yes. Python is totally flexible for whatever you want to do. Like overriding builtins too.
I'm working with Python 2.7 and PyGTK 2.24. I am working with the following tutorial. Please read it for code context.
http://www.pygtk.org/pygtk2tutorial/sec-PackingDemonstrationProgram.html
The bottom block of code (reprinted below) is throwing the following error when I type it in (verbatum):
if __name__ =="__main__":
if len(sys.argv) != 2:
sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
sys.exit(1)
PackBox1(string.atoi(sys.argv[1]))
main()
usage: packbox.py num, where num is 1, 2, or 3.
Traceback (most recent call last): File "C:/GTKTutorial/packbox.py",
line 161, in
sys.exit(1) SystemExit: 1
Additionally, if I change the code to the following to overcome the first error, I get the next error message:
if __name__ =="__main__":
if len(sys.argv) != 1:
sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
sys.exit(1)
PackBox1(string.atoi(sys.argv[1]))
main()
Traceback (most recent call last): File "C:/GTKTutorial/packbox.py",
line 162, in
PackBox1(string.atoi(sys.argv[1])) IndexError: list index out of
range
What is wrong? How do I fix the code so I can work with the tutorial>
You need to call it from the command line with packbox.py 1, packbox.py 2, or packbox.py 3.
This will result in there being two arguments (the name of the program and the first thing you pass to it), so you won't trigger the sys.exit(1), and argv[1] will be a valid index access.
To run PackBox.py directly from IDLE,
REPLACE:
if __name__ =="__main__":
if len(sys.argv) != 2:
sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
sys.exit(1)
PackBox1(string.atoi(sys.argv[1]))
main()
WITH:
if __name__ == "__main__":
packbox = PackBox1(3)
main()
To see all three example widget arrangements, substitute argument (3) with arguments (1) & (2). Click on X to exit the window; the Quit buttons aren't connected in this code.