I want to jump from def number1 to def number2.
I tried this:
def number1():
print("from here to ")
number2()
number1()
def blablabla():
print("blablabla")
blablabla()
def number2():
print("here")
number2()
but I received this error:
Traceback (most recent call last):
File "C:\Users\i5 9400f\Documents\projetos python\test.py", line 4, in <module>
number1()
File "C:\Users\i5 9400f\Documents\projetos python\test.py", line 3, in number1
number2()
^^^^^^^
NameError: name 'number2' is not defined. Did you mean: 'number1'?
from here to
Process finished with exit code 1
I tried using the number2()
it did not work
Python just run your code from top to bottom sequentially so if you try to access to something that is only defined later you won't succeed. What you need to do is to define all the functions first then call them later :
def number1():
print("from here to ")
number2()
def blablabla():
print("blablabla")
def number2():
print("here")
number1()
blablabla()
number2()
def number1():
print("from here to ")
number2()
def number2():
print("here")
def blablabla():
print("blablabla")
number1()
blablabla()
number2()
###Before runnıng def functıons, you should put them at the top of your processes
Related
I have this problem. I have 2 files at the moment. I am trying to print the words "Hello World Trudy". I can't get around doing it. It keeps telling me I have an attribute error. What should I do to fix it?
Traceback (most recent call last):
File "C:\Users\Trudy\Desktop\PythonLearning\test2.py", line 7, in <module>
f.sayHello()
AttributeError: 'NoneType' object has no attribute 'C'
test1.py
def main():
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
if __name__ == "__main__":
main()
test2.py
import test1
def function2():
print ("World")
test1.main().C.function6()
function2()
You don't need a main function in test1 file. Just have the Class C in there.
test1.py
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
test2.py
import test1
def function2():
print ("World")
test1.C().function6()
function2()
I'm trying but it's not working. I have the following code line:
class Run:
def Method(self, choice):
print "%sZip :\t%s%s\n".decode('utf-8') % (Basic_Green, White, choice.start().LoadJson['zip'])
And this is variable, and is within another class, and another function:
class Host_Method:
def start(self):
My_API = requests.get("http://ip-api.com/json/%s" % socket.gethostbyname(sys.argv[2]))
LoadJson = json.loads(My_API.content)
The error:
Traceback (most recent call last):
File "InfoIP.py", line 78, in <module>
elif sys.argv[1] == "-h": Run().Method(Host_Method())
File "/Pentest/InfoIP2/Libraries/InfoIP_Functions.py", line 159, in Method
print "%sZip :\t%s%s\n".decode('utf-8') % (Basic_Green, White, choice.LoadJson['zip'])
AttributeError: Host_Method instance has no attribute 'LoadJson'
You probably want them to be stored in an instance variable (self....). And you probably want your start to be an __init__ method. Your corrected class could look like:
class HostMethod:
def start(self):
self.my_API = requests.get("http://ip-api.com/json/{0}".format(socket.gethostbyname(sys.argv[2])))
self.load_json = json.loads(self.my_API.content)
Then, you could do:
class Run:
def Method(self, choice):
print "{0}Zip :\t{1}{2}\n".decode('utf-8').format(Basic_Green, White, choice.load_json['zip'])
a = Run()
a.method(HostMethod())
See also:
https://docs.python.org/3.4/tutorial/classes.html
i have written down the code but it gives me an error. It gives the global error and says that it does not define the search. Please help me out guys.
def testSearch():
s = range(0,1000000)
raw_input('basic, -1')
print search(s,-1)
raw_input('binary, -1')
print search1(s,-1)
raw_input('basic, end')
print search(s,1000000)
raw_input('binary, end')
print search1(s,1000000)
s = range(0,10000000)
raw_input('basic, partway')
print search(s,1000000)
raw_input('basic, larger end')
print search(0,10000000)
>>> testSearch()
basic, -1
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
testSearch()
File "C:\Users\bangash\Documents\python files\lec8.py", line 17, in testSearch
print search(s,-1)
NameError: global name 'search' is not defined
So, where is your function search defined? You should define it as a function:
def search(a, b):
...
The function search has to be defined before the code in testSearch has been executed. The code in testSearch is executed when testSearch() is executed. So the following two will work
def search():
pass
def testSearch():
search()
testSearch()
or
def testSearch():
search()
def search():
pass
testSearch()
They both will work because, just before the testSearch() is executed, we got definition for search. But not this
def testSearch():
search()
testSearch()
def search():
pass
I am new in Python and I wrote the following code:
class Frazione:
def __init__(self, Numeratore, Denominatore=1):
mcd=MCD(Numeratore,Denominatore)
self.Numeratore=Numeratore/mcd
self.Denominatore=Denominatore/mcd
def MCD(m,n):
if m%n==0:
return n
else:
return MCD(n,m%n)
def __str__(self):
return "%d/%d" %(self.Numeratore, self.Denominatore)
def __mul__(self, AltraFrazione):
if type(AltraFrazione)==type(5):
AltraFrazione=Frazione(AltraFrazione)
return Frazione(self.Numeratore*AltraFrazione.Numeratore, self.Denominatore*AltraFrazione.Denominatore)
__rmul__=__mul__
Open shell at the same folder of Frazione.py:
>>> from Frazione import Frazione
end then
>>> f=Frazione(10,5)
When I press Enter, I receive this output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\Frazione.py", line 5, in __init__
mcd=MCD(Numeratore,Denominatore)
NameError: global name 'MCD' is not defined
PS. I apologize for my english!
MCD is a method of Frazione, but you're calling it as if it were a global function. The easiest (and cleanest, IMHO) fix is to just move it outside the class, because it doesn't need to access any class or instance members.
So:
def MCD(m, n):
if m % n == 0:
return n
else:
return MCD(n, m % n)
class Frazione:
# as before but without MCD
If you do want to keep it in the class, then you might rewrite it to be iterative instead of recursive and call it as self.MCD in __init__. That's a good idea anyway, as Python's support for recursion is rather weak.
I'm trying to use a global variable. I've declared it as global to begin with and the declare it at such at each mention, but I get a NameError after the first function completes. Here's the code, and I think I've gone stare crazy but I can't seem to find the problem.
def on_servername_insertatcursor(self, widget):
global output
output = StringIO.StringIO()
servername = widget.get_text()
output.write("USHARE_NAME="+servername+'\n')
def on_netif_changed(self, widget):
netif = widget.get_active_text()
global output
output.write("USHARE_IFACE="+netif+'\n')
def on_port_insertatcursor(self, widget):
global output
port = widget.get_text()
output.write("USHARE_PORT="+port+'\n')
def on_telprt_insertatcursor(self, widget):
global output
telprt = widget.get_text()
output.write("USHARE_TELNET_PORT="+telprt+'\n')
def on_dirs_insertatcursor(self, widget):
global output
dirs = widget.get_text()
output.write("USHARE_DIR="+dirs+'\n')
def on_iconv_toggled(self, widget):
global output
iconv = widget.get_active()
if iconv == True:
output.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
else:
output.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')
def on_webif_toggled(self, widget):
global output
webif = widget.get_active()
if webif == True:
output.write("USHARE_ENABLE_WEB="+"yes"+'\n')
else:
output.write("USHARE_ENABLE_WEB="+"no"+'\n')
def on_telif_toggled(self, widget):
global output
telif = widget.get_active()
if telif == True:
output.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
else:
output.write("USHARE_ENABLE_TELNET="+"no"+'\n')
def on_xbox_toggled(self, widget):
global output
xbox = widget.get_active()
if xbox == True:
output.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
else:
output.write("USHARE_ENABLE_XBOX="+"no"+'\n')
def on_dlna_toggled(self, widget):
global output
dlna = widget.get_active()
if dlna == True:
output.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
else:
output.write("USHARE_ENABLE_DLNA="+"no"+'\n')
def on_commit_clicked(self, widget):
commit = output.getvalue()
logfile = open('/home/boywithaxe/Desktop/ushare.conf','w')
logfile.write(commit)
def on_endprogram_clicked(self, widget):
sys.exit(0)
What's amazing is that when insertatcursor (coming from Gtk.TextBuffer.insert_at_cursor() ) is replace with activate, the code works perfectly, except I don't want to have the user have to press enter after every data input.
EDIT. The Traceback is as follows
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 58, in on_netif_changed
output.write("USHARE_IFACE="+netif+'\n')
NameError: global name 'output' is not defined
Having made the changes suggested by #jdi (Thank you btw, I see the logic behind that), the Traceback I get is as follows:
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 55, in on_netif_changed
OUTPUT.write("USHARE_IFACE="+netif+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 72, in on_iconv_toggled
OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 79, in on_webif_toggled
OUTPUT.write("USHARE_ENABLE_WEB="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 86, in on_telif_toggled
OUTPUT.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 93, in on_xbox_toggled
OUTPUT.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 100, in on_dlna_toggled
OUTPUT.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 105, in on_commit_clicked
commit = OUTPUT.getvalue()
NameError: global name 'OUTPUT' is not defined
Your code example doesn't require a global. Just remove it. The only time you need to use the global keyword in a function is if you are going to assign to it.
OUTPUT = StringIO.StringIO()
def on_servername_insertatcursor(self, widget):
servername = widget.get_text()
OUTPUT.write("USHARE_NAME="+servername+'\n')
def on_netif_changed(self, widget):
netif = widget.get_active_text()
OUTPUT.write("USHARE_IFACE="+netif+'\n')
def on_port_insertatcursor(self, widget):
port = widget.get_text()
OUTPUT.write("USHARE_PORT="+port+'\n')
def on_telprt_insertatcursor(self, widget):
telprt = widget.get_text()
OUTPUT.write("USHARE_TELNET_PORT="+telprt+'\n')
def on_dirs_insertatcursor(self, widget):
dirs = widget.get_text()
OUTPUT.write("USHARE_DIR="+dirs+'\n')
def on_iconv_toggled(self, widget):
iconv = widget.get_active()
if iconv == True:
OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
else:
OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')
Even if you wanted to be able to reset your StringIO object in a function, it still wouldn't require an assignment or global keyword:
def reset_output(self):
OUTPUT.seek(0)
OUTPUT.truncate()
Proof that it works
import StringIO
OUTPUT = StringIO.StringIO()
def foo():
OUTPUT.write('foo')
def bar():
OUTPUT.write('bar')
def print_output():
print OUTPUT.getvalue()
def reset_output():
OUTPUT.seek(0)
OUTPUT.truncate()
if __name__ == "__main__":
foo()
bar()
print_output()
reset_output()
print_output()
Output
$ python test.py
foobar
$
Try moving output = StringIO.StringIO() outside and above all the functions in the file.
Well, the problem with the question is unfortunate indenting :) Looking at PyGTK docs shows that the on_... functions shown are indeed methods of a class, not global functions, so the "global" variable is probably not really global but also a member of the class (just look at the self parameter in method definitions).
I gave a more detailed answer on askubuntu, here's a code snippet which shows what needs to be done:
class MyApp(gtk.Window):
output = None
def __init__(...):
...
self.output = StringIO.StringIO()
def on_servername_insertatcursor(self, widget):
servername = widget.get_text()
self.output.write("USHARE_NAME="+servername+'\n')
def on_netif_changed(self, widget):
netif = widget.get_active_text()
self.output.write("USHARE_IFACE="+netif+'\n')
There's absolutely no PyGTK-specific or signals-specific magic involved :)