Getting AttributeError: 'int' object has no attribute 'isnumeric' error - python

Here I'am trying to build a simple calculator using tkinter and I have used some number images as buttons,i want only numbers and mathematical characters to be entered in entry box,but when i press the number button i get AttributeError: 'int' object has no attribute 'isnumeric' error, I didn't get the solution for this problem:
here is my code and below code is function for tkinter button:
def press(n):
new=value.get()
if new=="Can't divide by zero" or new=="Can't perform operation":
new=''
if n.isnumeric() or n=='+' or n=='-' or n=='*' or n=='/' or n=='%' or n=='.':
new+=str(n)
value.set(new)

The python isnumeric() method expects a string and checks if the characters in the string are numeric. If you're already passing n into def press(n) as an integer there is no reason to check if it's numeric and it's expecting a string which is why you get the AttributeError: 'int' object has no attribute 'isnumeric'. Your input should be a string, not an int literal.

The python isnumeric() method expects a string and checks if the characters in the string are numeric
as bpiekars said, and you can try:
str(n).isnumeric()

Related

Python '<' not supported between instances of 'str' and 'int' [duplicate]

I encountered the error
'>' not supported between instances of 'str' and 'int'
while trying to print the below lines in Pandas dataframe
print (survey_df_clean.shape)
print (survey_df_clean[survey_df_clean['text']>30].shape)
Should I try to convert them to int and how would that work in this statement?
First make sure that all value of survey_df_clean['text'] is the same, if you want to convert as numeric, do this :
survey_df_clean['text'] = pd.to_numeric(survey_df_clean['text'])
Then do this
survey_df_clean.loc[survey_df_clean['text']>30].shape
This message suggests, that you try to compare a string object (str) with an integer (int).
The expression
survey_df_clean['text']
will probably return a string. Therefore, you cannot directly compare it with the number 30. If you want to compare the length of the entry, you can use the pandas.Series.str.len() operation as you can see here.
If this field should actuallty contain an integer, you can use this method (pandas.to_numeric) to cast it from str to int.
survey_df_clean['text'] might have NAN or str values in it some where.
to find out :
survey_df_clean['text'].isnull().sum()
if they are,first take care of them then apply
print (survey_df_clean[survey_df_clean['text']>30].shape)
I had the same error message when trying to use that conditional. What intrigued me was that the same command had run correctly on another notebook.
The difference was in how I read the csv file. This was the troublesome one:
df=pd.read_csv('data.csv')
And when I put the decimal argument it worked:
df=pd.read_csv('data.csv', decimal=',')
Obviously, it'll depend on how your data is organized. ;)
This is because values in 'text' column are of type str and you are comparing str with int.
You can do a quick check for getting type of 'text' column.
print(type(survey_df_clean['text'][:1][0]))
For comparing you can do as following
survey_df_clean[survey_df_clean['text'].astype(int)>30]

How to convert timestamp into integer?

I've got a value that is of the type:
'pandas._libs.tslibs.timestamps.Timestamp'
but I want to convert it into:
'int'
Simply using int() doesn't work and gives me the following error message:
int() argument must be a string, a bytes-like object or a number, not 'Timestamp'
Edit: to be clear, I only want the type to change to integral. However I want the number to stay the same.
You should use the class (pandas._libs.tslibs.timestamps.Timestamp) methods. pandas._libs.tslibs.timestamps.Timestamp.timestamp() returns the timestamp, however it is a float instead of an int.

Pyautogui - How to type random words or numbers ("error: object not iterable")?

I'm quite new to python. I want pyautogui to type a random number or word. I've tried it like this:
a = random.randint(1,10)
pyautogui.typewrite(a)
but it returns the following error:
TypeError: 'int' object is not iterable
Doesn't pyautogui support variables, or do I have to use another formatting?
Thanks a lot
pyautogui.typewrite accepts a string as its parameter. So if you cast the number (int) to a string it should print out fine:
a = random.randint(1,10)
pyautogui.typewrite(str(a))

how to return values from map function on dataframe

I am trying to return values from map function but instead it gives me the memory address. I tried using list, but then it gives me an error stating str object doesn't have an attribute decode. Is there a way out?
The first problem you mentioned is probably because you are accessing an objects name rather than applying a method.
But for this error Error: AttributeError: 'str' object has no attribute 'decode'
This error is already answered in here 'str' object has no attribute 'decode'. Python 3 error?
As it said here you are trying to decode an object that is already decoded. You have a str, there is no need to decode from UTF-8 anymore. If you remove the .decode() method from your chain it will be ok.

How to slice a unicode object?

I was trying to get a unicode string from my database and slice it to get part of it.
However I get:
TypeError: 'unicode' object is not callable
Code:
info = post.body(0)[0:50]
Here post.body(0) is a {unicode} variable.
I want the first fifty characters of it.
post.body is already a unicode object. Python is complaining that you are trying to call it with argument 0.
So, just try
info = post.body[0:50]

Categories