This is part of my code:
if ind_1<>0:
rbrcol=[]
brdod1=[]
for i in range(27):
if Add_Cyc_1[1,i]!=0:
rbrcol.append(Add_Cyc_1[0,i])
brdod1.append(Add_Cyc_1[1,i])
Probrani_1=vstack((rbrcol,brdod1))
pok=0
for i in (rbrcol):
pok+=1
broj1=0
for j in range(21):
if SYS_STATE_1[i,j]==0:
broj1+=1
if broj1 <= Probrani_1[1,pok-1]:
SYS_STATE_1[i,j]=123456
And when i run program i get this:
Traceback (most recent call last):
File "C:/Python26/pokusaj2.py", line 157, in <module>
for i in (rbrcol):
NameError: name 'rbrcol' is not defined
What i do wrong???
I think the real problem is the if at the very top. Your indenting is incorrect - the code as written won't run because the line after the if is not indented.
Assuming it is indented in the original code, then rbrcol is not initialized if ind_1 is 0 and as ghostdog says if the if statement never fires, then rbrcol would not be set at all.
just as the error says, "rbrcol" doesn't have value. check your for loop
for i in range(27):
if Add_Cyc_1[1,i]!=0: <----- this part doesn't get through
rbrcol.append(Add_Cyc_1[0,i])
brdod1.append(Add_Cyc_1[1,i])
Probrani_1=vstack((rbrcol,brdod1))
also, what is Add_Cyc_1 ? To assign multidimension list
Add_Cyc_1[1,i] should be Add_Cyc_1[1][i]
further, this
if ind_1<>0: <<--- if this is not true, then rbrcol will not be defined
rbrcol=[] << --- <> should be != , although <> its also valid, but now ppl use !=
brdod1=[]
Related
Here is the code for my question. I was trying to translate a pseudocode into python. The function, ExCamel, forms a return string, OutString, from a given string,InString,by:
1:Separating the original words(a word is assumed to start with a capital letter)
2:Converting all characters to lower case.
InString=str(input("Enter a statement: "))
def ExCamel(InString):
NextChar=chr()
OutString=str()
n=int()
OutString=""
return OutString
for n in range(1,len(ExCamel(Instring))):
NextChar=InString[n:n+1]
if NextChar==upper(NextChar):
if n>1:
OutString=OutString+""
NextChar=lower(NextChar)
OutString=OutString+NextChar
But it gives an output of:
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/George/TrynaCreate.py", line 7, in <module>
for n in range(1,len(ExCamel(Instring))):
NameError: name 'Instring' is not defined
This just seems like a silly mistake which happens with most of us:
you have defined
InString
but you are using In's'tring
Hope that helps :)
I have built a code for a codewars problem. I think it is correct but it shows me an error I don't understand.
Can you tell me what am I doing wrong?
import math
def waterbombs(fire, w):
s=""
countx=0
for i in fire:
if i=="x":
countx+=1
elif i=="Y":
countx=0
return sum(math.ceil(countx/w))
waterbombs("xxYxx", 3)
This is the error:
Traceback (most recent call last):
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 16, in <module>
waterbombs("xxYxx", 3)
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 13, in waterbombs
return sum(math.ceil(countx/w))
TypeError: 'int' object is not iterable
[Finished in 0.2s]
Why are you doing sum(math.ceil(countx/w)) ?
What is the objective of the sum method here, since there is only value returned by math.ceil ?
The sum would throw that error if you pass a single value to it. You're supposed to pass a list of values of the sum method.
For eg: sum(5) would give you the same error you see above, but sum([5]) would return you 5.
I have this for-loop:
for i in range(1000000000, 1000000030):
foo(i)
When I execute it this error is given:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 11, in <module>
for i in range(1000000000, 10000000030):
OverflowError: range() result has too many items.
As far as I know, this range-object should have exactly 30 elements...
Where is the problem?
Edit:
I have removed the extra zero, now I get this:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 12, in <module>
factorizeInefficient(i)
MemoryError
Edit 2:
def factorizeInefficient(n):
teiler = list()
for i in range(n):
if i != 0:
if (n%i)==0:
teiler.append(i)
print teiler
Just found the solution myself: There is a range(n) object in this as well and this causes the memory Error...
An extra question: How did you guys know this was python 2? (Btw you were right...)
Copy/pasting the range() part of your code:
>>> len(range(1000000000, 10000000030))
9000000030
So there are actually about 9 billion elements in the range. Your first argument is presumably missing a zero, or second argument has a zero too many ;-)
count your zeros once again ;) I'd say it's one too much.
We just learned for loops in class for about five minutes and we were already given a lab. I am trying but still not getting what I need to get. What I am trying to do is take a list of integers, and then only take the odd integers and add them up and then return them so if the list of integers was [3,2,4,7,2,4,1,3,2] the returned value would be 14
def f(ls):
ct=0
for x in (f(ls)):
if x%2==1:
ct+=x
return(ct)
print(f[2,5,4,6,7,8,2])
the error code reads
Traceback (most recent call last):
File "C:/Users/Ian/Documents/Python/Labs/lab8.py", line 10, in <module>
print(f[2,5,4,6,7,8,2])
TypeError: 'function' object is not subscriptable
Just a couple of minor mistakes:
def f(ls):
ct = 0
for x in ls:
# ^ Do not call the method, but just parse through the list
if x % 2 == 1:
ct += x
return(ct)
# ^ ^ parenthesis are not necessary
print(f([2,5,4,6,7,8,2]))
# ^ ^ Missing paranthesis
You're missing the parenthesis in the function call
print(f([2,5,4,6,7,8,2]))
rather than
print(f[2,5,4,6,7,8,2])
Each line represents a single student and consists of a student number, a name, a section code and a midterm grade, all separated by whitespace.
The first parameter is already done and the file is open and
The second parameter is a section code
this is the link http://www.cdf.toronto.edu/~csc108h/fall/exercises/e3/grade_file.txt
My code:
def average_by_section(the_file, section_code):
'''(io.TextIOWrapper, str) -> float
Return the average midtermmark for all students in that section
'''
score = 0
n = 0
for element in the_file:
line = element.split()
if section_code == line[-2]:
mark = mark + float(line[-1])
n += 1
lecture_avg = mark / n
return lecture_avg
I'm getting an index out of range. Is this correct? Or am I just opening up the wrong file?
can someone test this code and download that file? I'm pretty sure it should work, but not for me.
Well, you can troubleshoot the index out of range error with a print line or print(line) to explore the number of items in "line" (i.e. the effect of split()). I'd suggest looking closer at your split() statement...
It looks like you are omitting parts of your code where you define some of those variables (section_code, mark, etc.), but adjusting for some of those things seems to work properly. Assuming that the error you got was IndexError: list index out of range, that happens when you try to access an element of a list by index where that index doesn't exist. For instance:
>>> l = ['one']
>>> l[0]
'one'
>>> l[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> l[-1]
'one'
>>> l[-2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Therefore in your code, you will get that error if line is ever fewer than two items. I would check and see what you are actually getting for line to make sure it is what you expect.