Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hi I am writing a Monopoly game simulator and have the following list of
Community Chest Card Nos within a card object :-
self.CChcards_MessNo = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
I want to shuffle these,with the following method
def shuffle(self):
import random
random.shuffle(self.CChcards_MessNo)
which works early on in the program but fails and gives
the following message later in the main part of the program.
File "C:\Users\David\AppData\Local\Programs\Python\Python35\lib\random.py", line 278, in shuffle
for i in reversed(range(1, len(x))):
TypeError: object of type 'int' has no len()
This occurs when the program has cycled through the 16 cards and now needs to shuffle the cards
>>> class Foo():
... def __init__(self):
... self.CChcards_MessNo = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
... def shuffle(self):
... import random
... random.shuffle(self.CChcards_MessNo)
... def bug(self):
... print("I'm a bug that makes shuffle() fail by assigning an int to self.CChcards_MessNo")
... self.CChcards_MessNo = 0
...
>>> foo = Foo()
>>> foo.shuffle()
>>> foo.bug()
I'm a bug that makes shuffle() fail by assigning an int to self.CChcards_MessNo
>>> foo.shuffle()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in shuffle
File "C:\Program Files (x86)\Python36-32\lib\random.py", line 271, in shuffle
for i in reversed(range(1, len(x))):
TypeError: object of type 'int' has no len()
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have been working with Flask.route() decorator for a while and wanted to write my own, but it always tells me that the function isn't passed into it.
I've copied everything exactly like in the Flask examples, so my decorator definition must be wrong:
def decorator(f, *d_args):
def function(*args, **kwargs):
print('I am decorated')
return f(*args, **kwargs)
return function
#decorator()
def test(a, b=1):
print('Test', a, b)
test(1, 6)
The error I get:
Traceback (most recent call last):
File "C:/Users/Tobi/Desktop/decorators.py", line 49, in <module>
#decorator()
TypeError: decorator() missing 1 required positional argument: 'f'
First of all, there are questions on SO which handle this problem. You should have done more research on this error before writing a new question. But anyway:
The reason for your error is because you are calling the decorator by writing it before a def because you are already calling it using the brackets decorator() without passing anything in, it throws an error.
For your decorator, the correct usage would be:
#decorator # no brackets here
def function()
...
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm new to python, and I'm using PyCharm. I met a problem when trying to access a list in an object.
I have a file to store the list object, let's say Box.py, and a main file to run the programme, Main.py.
And this is what is inside my Box.py:
class Container:
def __int__(self):
self.list = ["1", "2", "3"]
And now I want to print the list in the class Container from my Main.py.
This is what I wrote for my Main.py:
from MainProgramme import Container
def main():
container = Box.Container()
print(container.list)
if __name__ == "__main__":
main()
So, I have created an object container, and use it to access the list in the object.
But when I run the Main.py, there was an error saying:
Traceback (most recent call last):
File "D:\PyCharm\MyProject\MainProgramme\Main.py", line 8, in <module>
main()
File "D:\PyCharm\MyProject\MainProgramme\Main.py", line 5, in main
print(container.list)
AttributeError: 'Container' object has no attribute 'list'
May I know what is the problem, and how do I solve it?
You have a syntax error, you aren't actually calling init.
def __int__(self):
should be
def __init__(self):
Also, to run your code, I had to change the creation of the Container object.
container = Box.Container()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
class Solution:
def remove(self,arr,target):
if target in arr:
arr.remove(target)
remove(arr,target)
return len(arr),arr
else:
return "not in the array"
ans=Solution()
print(ans.remove([3,2,2,3],3))
This is the Error
Traceback (most recent call last):
File "c:\Users\ashut\Practice\scrap.py", line 10, in <module>
print(ans.remove([3,2,2,3],3))
File "c:\Users\ashut\Practice\scrap.py", line 5, in remove
remove(arr,target)
NameError: name 'remove' is not defined
Somehow the above program runs in google colab and I've tried restarting the runtime
You have to add the "self" tag before calling the function inside ur function.
class Solution:
def remove(self,arr,target):
if target in arr:
arr.remove(target)
self.remove(arr,target)
return len(arr),arr
else:
return "not in the array"
ans=Solution()
print(ans.remove([3,2,2,3],3))
The temp variable name is arr, is that mean Array?
If so, the code "remove(arr, target)" is no need, remove this line, is OK.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
While testing some code, I was given an error:
TypeError: breadth_first_search() takes from 2 to 3 positional arguments but 4 were given
The parameter bit of the function declaration looks like this:
def breadth_first_search(self, id: int, level=None)
The call looks like this:
tree.breadth_first_search(parent_id)
As far as I'm aware, this should be correct. I don't know why it would interpret my one argument (or two, including self) as four. Is there something plain that I'm missing?
--
For completeness, here's the traceback:
Traceback (most recent call last):
File "test.py", line 4, in <module>
tree = FeatureQuery.load_feature_tree("general", inventory)
File "D:\Speechcraft\Python\core\ling_query.py", line 201, in load_feature_tree
FeatureQuery.load_feature_node_recursive(feature_inventory, tree, results, l)
File "D:\Speechcraft\Python\core\ling_query.py", line 221, in load_feature_node_recursive
parent = tree.breadth_first_search(parent_id)
File "D:\Speechcraft\Python\core\phonological_units.py", line 37, in breadth_first_search
return self.breadth_first_search(self, id, next_level)
TypeError: breadth_first_search() takes from 2 to 3 positional arguments but 4 were given
It turned out to be a silly mistake. I accidentally included self as a parameter in the recursive call. Thanks go to user2357112 supports monica.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have two codes with the same structure but different task. One of them generates the correct answer but the other generates a TypeError:
TypeError: int() argument must be a string or a number, not 'generator'
# The correct one
def square_digits(num):
return int(''.join(str(int(i)**2) for i in str(num)))
# The one generates Type error
def mirror(num):
return int(''.join(str(num)[i]) for i in range(len(str(num))-1,-1,-1))
I think the way your functions are written might be confusing and unclear. This might also prevent you from realizing where the error comes from, so I took your code and turned
def square_digits(num):
return int(''.join(str(int(i)**2) for i in str(num)))
# The one generates Type error
def mirror(num):
return int(''.join(str(num)[i]) for i in range(len(str(num))-1,-1,-1))
into equivalent code but I replaced the generator object with a regular for loop:
def square_digits(num):
result = []
for i in str(num):
result += [str(int(i)**2)]
result = ''.join(result)
result = int(result)
return result
# The one generates Type error
def mirror(num):
result = []
for i in range(len(str(num))-1,-1,-1):
result += [''.join(str(num)[i])]
result = int(result)
return result
Since the code itself is equivalent, mirror still produces the same error:
Traceback (most recent call last):
File "test.py", line 27, in <module>
print(mirror(12))
File "test.py", line 22, in mirror
result = int(result)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
When you call
result = int(result)
inside the mirror function, result is still a list. That's where the error comes from.
When you look at this unraveled code, is this still what you wanted to write or do you spot the mistake in your algorithm?