Can someone tell me what i am doing wrong? I am writing a program using loops in Python 3.x, but when i execute program i am getting a traceback error:
multiple of 13 is 195 and factors are as follows
Traceback (most recent call last):
File "C:/Users/Darlene/Desktop/Chapter 4/program4_2.py", line 19, in
list1.append(j)
AttributeError: 'dict' object has no attribute 'append'
this is the code i entered:
def main():
for i in reversed(list(range(100,201))):
if i%13==0:
print("multiple of 13 is",i,"and factors are as follows")
list1 = {}
for j in list(range(2,i+1)):
if i%j == 00:
list1.append(j)
print(list1)
main()
As commented by Luke Park, list1 = {} will declare a dictionary. What you need is list1 = [].
Also, range will already return a range type that can be handled by most methods and loops so there's no need to cast it to a list.
list1 must be an list like so...
list1 = []
you defined it as an dict, and as python said
'dict' object has no attribute 'append'
Related
I am trying to add suffix to an existing array. Below is my code
print('a' + [10, 100])
With this I am getting below error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str
Could you please help hoe to do that? I could use some for loop but I believe there may be more straightforward way to achieve the same.
You can create a new concatenated array as:
>>> ['{0}{1}'.format('a', num) for num in [10, 100]]
['a10', 'a100']
Read String format and List Comprehensions from doc.
If I understand your question, you want a new string array (list). You could try this:
new_lst = ['a'+str(x) for x in [10, 100]] # just use string concatentation
I am trying to loop on an Array of Hashes. When I reach the point where I fetch the Enumerator to start looping, I get the following error:
undefined method `[]' for nil:NilClass
My code looks like the following:
def extraireAttributs (attributsParam)
classeTrouvee = false
scanTrouve = false
ownerOSTrouve = false
ownerAppTrouve = false
resultat = Hash.new(0)
attributs = Array(attributsParam)
attributs.each do |attribut| #CRASHES HERE!!!
typeAttribut = attribut['objectTypeAttribute']
[...]
I checked in debug mode to make sure the attributsParamsargument and the attributsvariable are not nil or empty. Both (because they are the same!) contain 59 Hashes objects, but I still cannot get an Enumerator on the Array.
Why do I keep on getting this error?
Thanks!
undefined method `[]' for nil:NilClass says you tried to do something[index] but something is nil. Ruby won't let you use nil as an array (ie. call the [] method on it).
The problem is not on the attributs.each line but on the line following which calls the [] method on attribut.
typeAttribut = attribut['objectTypeAttribute']
This indicates something in attributs is nil. This could happen if attributsParam is a list that contains nil like so.
attributsParam = [nil];
attributs = Array(attributsParam);
# [nil]
puts attributs.inspect
Simplest way to debug it is to add puts attributs.inspect just before the loop.
Also consider if you really need the attributs = Array(attributsParam) line or if it's already something Enumerable.
I am aware that there is a similar question for Z3 C++ API, but I couldn't find corresponding information for Z3Py. I'm trying to retrieve arrays from models found by Z3, so that I can access the array's values using indexes. For instance, if I had
>>> b = Array('b', IntSort(), BitVecSort(8))
>>> s = Solver()
>>> s.add(b[0] == 0)
>>> s.check()
sat
then I'd like to do something like
>>> s.model()[b][0]
0
but I currently get :
>>> s.model()[b][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'FuncInterp' object does not support indexing
Judging from the C++ answer, it seems like I'd have to declare a new function using some values I got from the model, but I don't understand it well enough to adapt it to Z3Py myself.
You can ask the model to evaluate (eval(...)) the array at a particular point by constructing a call to the associated array model function. Here's an example:
b = Array('b', IntSort(), BitVecSort(8))
s = Solver()
s.add(b[0] == 21)
s.add(b[1] == 47)
s.check()
m = s.model()
print(m[b])
print(m.eval(b[0]))
print(m.eval(b[1]))
which produces
[1 -> 47, 0 -> 21, else -> 47]
21
47
So what I'm trying to do is create a function that reads in a list and create a new list without the punctuation, using loops.
So far, I've got:
list=["This:","is","a","list."]
def depunctuate():
for i in range(0,len(list),1):
list1=""
for j in range(0,len(list[i]),1):
if(list[i][j] !=['(',')','?',':',';',',','.','!','/','"',"'"]):
list1+=list1[i][j]
cleanList+=[list1]
return cleanList
depunctuate()
So what I'm looking for it to return is "This is a list"
However I'm getting
Traceback (most recent call last):
File "depunctuate.py", line 10, in <module>
depunctuate()
File "depunctuate.py", line 7, in depunctuate
tokens1 += tokens1[i][j]
IndexError: string index out of range
Any help is appreciated, thanks!
clean_l = [s.strip(".:;,'\"?!/") for s in l]
This will remove leading and trailing punctuation chars.
I met quite strange behaviour of each method on list of arrays in groovy.
I have given piece of code.
def list = [
[2, "foo"].toArray(),
[4, "bar"].toArray()
]
list.each { def row ->
println(row.length)
}
Which gives me pretty expecting result in console
2
2
Then I did small modification to this code
def list = [
[2, "foo"].toArray(),
[4, "bar"].toArray()
]
list.each { Object[] row ->
println(row.length)
}
And result is
1
1
Because variable row is array with one element which is my original 2 elements array from list.
Is there some explanation for this?
Im using groovy 1.8.8 or 2.1.2
I guess it's a feature rather than a bug :p
Object[] in a closure declaration has a special semantic for variable argument:
From http://groovy.codehaus.org/Closures+-+Formal+Definition:
Groovy has special support for excess arguments. A closure may be declared with its last argument of type Object[]. If the developer does this, any excess arguments at invocation time are placed in this array. This can be used as a form of support for variable numbers of arguments.
In your example, the argument passed to the closure will be wrapped again with a new Object array, containing list as the only element.
As an example:
def list = [
[2, "foo"].toArray(),
[4, "bar"].toArray()
]
def c = {Object[] args -> println args}
c(list)
Output:
[[[2, foo], [4, bar]]]