Python error message - python

How can I fix this error:
Traceback (most recent call last):
File "C:\Users\Tony\Desktop\Python\4.py", line 64, in
print "YOU PAY: $",(pc-total)
TypeError: unsupported operand type(s) for -: 'str' and 'float'

One of the two, pc or total, is a float and the other a string.
As python is strongly typed you would need to cast the string to a float, e.g.:
print "YOU PAY $",(float(pc) - total)

Related

TypeError: cannot concatenate 'str' and '_Printer' objects

I would like to know why print("Int to string: "+str(A_GLOBAL_INT_VARIABLE_CALLED_FROM_A_FUNCTION)) gives me this error: TypeError: cannot concatenate 'str' and '_Printer' objects any help?
Full error:
Traceback (most recent call last):
File "main.py", line 96, in <module>
gMenu()
File "main.py", line 83, in gMenu
terminalWarn("ACCOUNT DETAILS [DEBUG]:\n\nNickname: "+nickname+"\n\nExperiance: "+str(expe)+"\n\nLevel: "+str(level)+"\n\nCreator: "+creator+"\n\nCopyrights: "+copyright,"exit to menu")
TypeError: cannot concatenate 'str' and '_Printer' objects
(all variables defined in the error are ran in a function and are global.)

fnv 0.2.0 Usage TypeError

I am trying to use fnv hash function on python-3.6, but I am getting an error
Traceback (most recent call last):
File "C:/Users/SACHIN/AppData/Local/Programs/Python/Python36/bloom.py", line 4, in module
fnv.hash(data, algorithm=fnv.fnv_1a, bits=64)
File "C:\Users\SACHIN\AppData\Local\Programs\Python\Python36\lib\site-packages\fnv__init__.py", line 52, in hash
OFFSET_BASIS[bits]
File "C:\Users\SACHIN\AppData\Local\Programs\Python\Python36\lib\site-packages\fnv__init__.py", line 28, in fnv_1a
return ensure_bits_count((hash_value ^ byte) * PRIMES[bits], bits)
TypeError: unsupported operand type(s) for ^: 'int' and 'str'
For code
import fnv
data = 'my data'
fnv.hash(data, algorithm=fnv.fnv_1a, bits=64)
fnv.hash(data, bits=64)
fnv.hash(data, algorithm=fnv.fnv, bits=64)
which is exactly copied from https://pypi.python.org/pypi/fnv/0.2.0
Please let me know what actually is wrong.
Just ran into this error today. I got around it by encoding the string. For example, the below should all work.
import fnv
data = 'my data'
fnv.hash(data.encode(), algorithm=fnv.fnv_1a, bits=64)
fnv.hash(data.encode('ascii'), bits=64)
fnv.hash(data.encode('utf-8'), algorithm=fnv.fnv, bits=64)

Python 3: Is there a way to get the line number of an error and the error statement when using exec [duplicate]

This question already has an answer here:
how to get the line number of an error from exec or execfile in Python
(1 answer)
Closed 7 years ago.
Is there a way to get the line number of the error through executing a file? Let's say that I have the following code:
exec(open("test.py").read())
And the file has an error, in which IDLE pops up the following:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
return self.func(*args)
File "C:\Users\henrydavidzhu\Desktop\Arshi\arshi.py", line 349, in runFile
exec(open(self.fileName).read())
File "<string>", line 2, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
I want to know the error statement (TypeError: unsupported operand type(s) for +: 'int' and 'str'), and the line number. This is not a duplicate because I am using Python 3, and not Python 2.
You can try this code:
import sys
import traceback
try:
exec(open('test.py').read())
except Exception as e:
exc_type, ex, tb = sys.exc_info()
imported_tb_info = traceback.extract_tb(tb)[-1]
line_number = imported_tb_info[1]
print_format = '{}: Exception in line: {}, message: {}'
print(print_format.format(exc_type.__name__, line_number, ex))

TypeError: unsupported operand type(s) for /: 'float'. Simple plot-drawing program

I'm writing a program that draws Lennard-Jones potential with parameters adjusted in GUI with sliders.
This is my code:
from Tkinter import *
import pylab as p
import math
def show_values():
V=epsilon.get*(math.exp(-r/sigma.get)-(2/sigma.get)**6)
p.plot(r,V)
p.show()
r = p.arange(0.1, 0.2, 0.01)
master = Tk()
epsilon = Scale(master, from_=-10,length=300, to=30, resolution=0.1, width=100)
epsilon.pack()
sigma = Scale(master, from_=-50, to=25, length=300,resolution=0.1, orient=HORIZONTAL)
sigma.pack()
Button(master, text='Show', command=show_values).pack()
mainloop()
But I receive this error message from my IDE (canopy)
%run C:/Users/PC/Desktop/lenard.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.4.1.1975.win-x86\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Users\PC\Desktop\lenard.py", line 7, in show_values
V=epsilon.get*(math.exp(-r/sigma.get)-(2/sigma.get)**6)
TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'
So my question has three parts:
What does this message mean?
How to make this program work?
Is "error message" proper word? How do we call such messages?
Regarding each of your questions:
The error message means that you are trying to divide a float object by an instance-method (function) object.
Because get is an instance-method of the Scale class, you must call it as such:
V=epsilon.get()*(math.exp(-r/sigma.get())-(2/sigma.get())**6)
# ^^ ^^ ^^
Otherwise, you will be performing your calculations with the get function object itself.
Yes, you may call it that. The term "traceback" usually refers to the entire error output:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.4.1.1975.win-x86\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Users\PC\Desktop\lenard.py", line 7, in show_values
V=epsilon.get*(math.exp(-r/sigma.get)-(2/sigma.get)**6)
TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'
while "error message" usually refers to only the last line:
TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

storing result of MySQL query into python variable

when I execute the following code using python programming language and MySQL database
cursor.execute("select max(propernoun_SRNO) from tblauto_tagged")
starting_index = cursor.fetchone()
ending_index = starting_index +len(s)
I get following error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
batch(1,1)
File "C:\Users\vchauhan\Dropbox\Code\proper_noun_function_batch_file_mysql_sept_12.py", line 97, in batch
ending_index = starting_index +len(s)
TypeError: unsupported operand type(s) for +: 'pyodbc.Row' and 'int'
Problem
The problem here is that you are assigning pyodbc.Row instance (returned by .fetchone()) to starting_index, which makes it impossible to add it to the integer (thus the "TypeError: unsupported operand type(s)" error).
Solution
Try to replace this line:
starting_index = cursor.fetchone()
with this line:
starting_index = cursor.fetchone()[0]
More reading
PEP 249 - Python Database API Specification v2.0 (especially part about fetchone())

Categories