The code :
x=0
def ex():
u= input (': ')
if u =='a':
print ('okay')
x = x + 1
ex()
The error :
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
ex ()
File "<pyshell#2>", line 5, in ex
x = x + 1
UnboundLocalError: local variable 'x' referenced before assignment
This is what I get. I have no idea what's wrong. Thanks in advance
Related
print ('welcome to the new world')
print('what is your name')
myName = input()
print ('it is good to meet you , ' + myName)
print (' Th length of your name is : ')
print (len(myName))
print('what is your age')
myEdge = input()
print ('you were born on ,')
print (2018 - myEdge)
The above code fails at last line.
Traceback (most recent call last):
File "C:\Users\Pratik\Desktop\python\First_Program.py", line 10, in <module>
print (2018 - myEdge)
TypeError: unsupported operand type(s) for -: 'int' and 'str'
But I can run it manually by assigning value and it is running while we convert the variable data type to string print (2018 - int(myEdge))
confused why difference between script and command line execution
myEdge = 29
print ( 2018 - myEdge )
1989
In line command :
myEdge = 29 # You put an integer in the variable
In script :
myEdge = input("Enter your age -> ") # You put a string in the variable
It's why you have a difference between line command and script.
You have the solution for the script int(myEdge) . For the tips you can add text into the input : input("add_the_text_here") .
Moreover in line command you can test the same :
>>> t = input(">")
>29
>>> type(t)
<class 'str'>
>>> 30 - t
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
30 - t
TypeError: unsupported operand type(s) for -: 'int' and 'str'
I'm getting error for line 1 not sure what the problem is
Traceback (most recent call last):
File "python", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'mars' is not defined
Here's the code:
x = int(input("what is the fourth planet in the solar system: "))
if x == mars:
print ("correct")
else print ("incorrect")
You forgot the quotes around mars just like this 'mars'
This code is for the gauss elimination that works with a matrix and I can't understand why the following error appears on screen.
def elimGauss(A,m,n):
for k in range(n-1):
for i in range(k+1,n):
p = -A[i][k] / (A[k][k])
for j in range(k,n):
if (k == j):
A[i][j] = 0
else:
A[i][j] = A[i][j] + (p * A[k][j])
The error is:
Traceback (most recent call last):
File "C:/Users/Solange/Desktop/Trabajo Final Fund. Programacion - Eliminacion Gauss.py", line 42, in <module>
elimGauss(M,3,3)
File "C:/Users/Solange/Desktop/Trabajo Final Fund. Programacion - Eliminacion Gauss.py", line 29, in elimGauss
p = -A[i][k] / (A[k][k])
TypeError: 'NoneType' object is not subscriptable
I am trying to debug this function:
def check_domains(url):
global num_websites,domain_queue,domains,doc_queue,stanford_tagger
the_domain = re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$',url)
if the_domain is not None:
if the_domain.groups(0)[1] not in domains.keys():
domains[the_domain.groups(0)[1]] = website(doc_queue,the_domain.groups(0)[1])
domains[the_domain.groups(0)[1]].add_initial_url(url)
domain_queue.append(domains[the_domain.groups(0)[1]])
num_websites = num_websites + 1
else:
domains[the_domain.groups(0)[1]].add_url(url)
error:
File "web_crawler.py", line 178, in getdoc
check_domains(check)
File "web_crawler.py", line 133, in check_domains
the_domain = re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$',url)
File "/usr/local/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
I try and be a good boy and test in interactive mode:
>>> def check_domains(url):
... the_domain = re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$',url) #right here
... if the_domain is not None:
... print the_domain.groups(0)[1]
... else:
... print "NOOOO!!!!!"
...
>>>
>>> check_domains("http://www.hulu.com/watch/6704")
hulu.com
>>> check_domains("https://docs.python.org/2/library/datetime.html")
python.org
so this does what I want it to do and I didn't change that line. But why???
The value of url can still be None and that's what gives this error:
>>> re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$', None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
So you should check whether the object that you're passing for url is indeed a string. It may even be a number or something else but it's not a string which is what the matching function expects.
I'm trying to print the EXIF of an image.
This is my code:
with Image(filename="/home/hapoofesgeli/Desktop/b.jpg") as image:
for k, v in image.metadata.items():
if k.startswith('exif:'):
print(k, v)
But it it gives an error:
Traceback (most recent call last):
File "/home/hapoofesgeli/Programming/Test/Test.py", line 5, in <module>
for k, v in image.metadata.items():
File "/usr/lib/python3.3/collections/abc.py", line 480, in __iter__
yield (key, self._mapping[key])
File "/usr/lib/python3.3/site-packages/wand/image.py", line 2260, in __getitem__
raise TypeError('k must be a string, not ' + repr(format))
TypeError: k must be a string, not <built-in function format>
How to solve this error?
you should use the _getexif() method of that comes bundled in PIL's Image module:
>>> image = Image.open(os.getcwd() + '/canon-ixus.jpg')
>>> image._getexif()
{36864: '0210', 37121: '\x01\x02\x03\x00', .... }
or also image.info['exif']:
>>> image.info['exif'][0:20]
'Exif\x00\x00II*\x00\x08\x00\x00\x00\t\x00\x0f\x01\x02\x00'