I'm getting 'int' object is not subscriptable - python

I'm new to python I'm trying to implement a code for my project at first my error code was object of type 'int' has no len() this was my code and added str to solve the issue
xored_value = ord(Mblocks[i%len(Mblocks)]) ^ ord(Cblocks[i%len(Cblocks)])
Now I'm getting 'int' object is not subscriptable if in this line
xored_value = ord(Mblocks[i%len(str(Mblocks))]) ^ ord(Cblocks[i%len(str(Cblocks))])
If I change it to
xored_value = ord(Mblocks(i%len(str(Mblocks)))=)) ^ ord(Cblocks(i%len(str(Cblocks))))
I get 'str' object is not callable.
Here's my full function :
def xor_two_str(Mblocks,Cblocks):
xored = []
for i in range(max(len(str(Mblocks)), len(str(Cblocks)))):
xored_value = ord(Mblocks[i%len(str(Mblocks))]) ^ ord(Cblocks[i%len(str(Cblocks))])
xored.append(hex(xored_value)[2:])
return ''.join(xored)
Any help please?

So, I am sure the Mblocks and Cblocks parameters you are passing are integers. Since it is being an integer, if you try to slice part of it you will get TypeError
> 100[1]
TypeError: 'int' object is not subscriptable
Lets do a piece by piece inspection.
Here in the first approach:
> xored_value = ord(Mblocks[i%len(Mblocks)]) ^ ord(Cblocks[i%len(Cblocks)])
> error1 = len(Mblocks)
TypeError: object of type 'int' has no len()
since Mblocks is integer, integer doesn't have len function
In change 2,
you corrected the first error but:
> xored_value = ord(Mblocks[i%len(str(Mblocks))]) ^ ord(Cblocks[i%len(str(Cblocks))])
> error2 = Mblocks[i%len(str(Mblocks))]
> error2 = Mblocks[some_int]
TypeError: 'int' object is not subscriptable
In change 3:
xored_value = ord(Mblocks(i%len(str(Mblocks)))=)) ^ ord(Cblocks(i%len(str(Cblocks))))
> error3 = Mblocks(callingWithParameter)
Simply in python something(withbraces) is calling something. Same happened here
So the easiest solution is make Mbraces and Cbraces of before you process anything down Like here is the solution:
def xor_two_str(Mblocks,Cblocks):
Mblocks = str(Mblocks)
Cblocks = str(Cblocks)
xored = []
for i in range(max(len(Mblocks), len(Cblocks))):
xored_value = ord(Mblocks[i%len(Mblocks)]) ^ ord(Cblocks[i%len(Cblocks)])
xored.append(hex(xored_value)[2:])
return ''.join(xored)

Python expects strings for the ord() function, and this:
Mblocks[i%len(str(Mblocks))]
is an attempt to access element with index i%len(str(Mblocks)) from int Mblocks, which Python does not allow.
As such, you could do a str conversion at the beginning of your function and work with the converted variables from that point onwards.
def xor_two_str(Mblocks,Cblocks):
str_Mblocks=str(Mblocks)
str_Cblocks=str(Cblocks)
xored =[]
for i in range(max(len(str_Mblocks), len(str_Cblocks))):
xored_value = ord(str_Mblocks[i%len(str_Mblocks)]) ^ ord(str_Cblocks[i%len(str_Cblocks)])
xored.append(hex(xored_value)[2:])
return ''.join(xored)

Related

How to check if some datatype is string or list in python?

I'm trying to add a check here that if the receiver is just a string. then convert that into list else just pass.
if type(receiver) == str:
receiver=[receiver]
error:
TypeError: 'str' object is not callable
You can check the type of an instance with the following.
if isinstance(receiver, str):
# do something if it is a string
If you just need to check rather it is not a string:
if not isinstance(receiver, str):
# do something if it is not a string
Look at this tutorial to learn more about the function.
a = '123'
print(str(a))
print(type(a))

TypeError: '>' not supported between instances of 'float' and 'str'

What is the easiest way to get rid of this simple error:
'>' not supported between instances of 'float' and 'str'.
Some of the articles look really complex to solve this, but in my opinion, there should be an easy fix. Like putting currentTime==(str(currentTime)). Other things I attempted are at the bottom. My code :
df=pd.read_csv(file_name, header=None)
last3Rows=df.iloc[-3:]
for i in range(3):
lastRow = df.iloc[i]
tradeTime=lastRow[4]
currentTime=datetime.datetime.now().timestamp()
print (currentTime)
print(type(currentTime))
print (tradeTime)
print(type(tradeTime))
if currentTime > tradeTime:
print("inner loop reached")
What I tried:
currentTime = datetime.strptime('%H:%M:%S')
Gives :
AttributeError: module 'datetime' has no attribute 'strptime'
currentTime = strptime('%H:%M:%S')
Gives :
AttributeError: module 'datetime' has no attribute 'strptime'
currentTime=datetime.datetime.now().time()
Gives :
TypeError: '>' not supported between instances of 'datetime.time' and 'str'
The problem you have is the result of attempting to use the ">" operator with operands of different types. Thus, the easiest way to solve this is to convert them both to a type that has a valid greater-than operator implementation, like using the unix timestamp to cast to a float.
if float(currentTime.total_seconds()) > float(tradeTime.total_seconds()):
print("inner loop reached")

TypeError: argument of type 'int' is not iterable - python 3.5

While doing data transformation, I am getting "TypeError: argument of type 'int' is not iterable", I am using python 3.5
This is the code snippet,
for idx, val in enumerate(aircrashdf.Destination):
if ',' in val:
destination = val.split(",")
original = destination[0].strip()
aircrashdf.iloc[idx,10] = original
This is the output
Traceback (most recent call last):
File "/home/drogon/PycharmProjects/aircrashes.py", line 151, in <module>
if ',' in val:
TypeError: argument of type 'int' is not iterable
What could be the problem?
it seems to me that type of val variable is int, you can check type of any function and variable by using type() function..it will return the type of variable....you can also compare the type of variable by using following code...
'
# if type is same as you provided then it will return true
if isinstance(var_name, data_type):
# your code here
'
In the for loop, the val value is a type of integer and you are trying to search , in an integer.

TypeError, 'str' is not callable - Python

why does this code return a TypeError: 'str' object is not callable TypeError: 'str' object is not callable?
import string
def containsAny(stri, set):
"""Check whether 'str' contains ANY of the chars in 'set'"""
return 1 in [c in stri for c in set]
a = containsAny("acde",list(string.ascii_uppercase()))
print "{}".format(a)
string.ascii_uppercase is a string, therefore you get an error when trying to call it by adding the ().

TypeError: 'str' object is not callable

I am having a weird issue with Python. For some reason, when I call it from the command line, I can use the replace() function as much as I want but I cannot use it inside a specific class. The code yields the following (well-known) error:
File "/homes/mmj11/malice/lexer.py", line 96, in replaceInTree
tree[i] = tree[i].replace(" "," ")
TypeError: 'str' object is not callable
The function I'm using is the following:
def replaceInTree(self, tree):
for i in range(len(tree)):
if type(tree[i] is str):
tree[i] = tree[i].replace(" "," ")
tree[i] = tree[i].replace(tree[i], self.getToken(tree[i]))
else:
tree[i] = self.replaceInTree(tree)
return tree
I really think this should not happen as I can do the exact same thing in the command line. I am very sure that str's are meant to be callable.
Instead of
if type(tree[i] is str):
Dont you mean to do
if type(tree[i]) is str:
I would do this:
if type(tree[i]) is str:
instead of this:
if type(tree[i] is str):
The way you have it written evaluates to if type(false), which is equivalent to if bool, which will always be truthy.

Categories