Use of For _ in range() - python

I am a newcomer to Python, and I'm using anaconda/jupyter.
Have been unable to decipher the following error message:
sims=[500]
For _ in range(1000):
File "<ipython-input-264-b9c4f88d1aeb>", line 1
for i in range(sims):
^
SyntaxError: unexpected EOF while parsing
In know the meaning of EOF but cannot find he error.
Help will be greatly appreciated

Considering the previous comments:
Your variable sims doesn't have to be a list because it is just a single number, or maybe you can call it later as for i in range(sims[0]):
Your code is finishing after that for, set for i in range(sims[0]):pass to let Python to know it is ending.
You have an uppercase in your first For, change it to for

Related

Invalid syntax, Unexpected EOF

What is wrong with my code?
It says there is an invalid syntax and highlighting the colon?
BTW, I'm doing computing GCSE and this is part of the coursework prep.
I want it to take a letter to repeat and then repeat it an inputted number of times.
letter=input("Please enter a letter to be repeated: ")
number=int(input("Please enter the number of times you want it repeated: ")
for a in range(0,number):
print(+letter)
Remember that when you're debugging, compilers and interpreters will report where an error was first detected, not necessarily where the error actually is. You're missing a closing parentheses on this line:
number=int(input("Please enter the number of times you want it repeated: ")
Add another ) to the end of that line. The interpreter is seeing the opening parentheses for the int function call, then is happily looking through the rest of the file to find its match. When it reaches the end of the file without the parentheses being balanced, it gives up and throws the exception.
As Josh points out, +letteris also invalid syntax. I'm not sure what you were trying to achieve with it so I can't recommend a specific fix, but it needs to go.
You are missing your closing parenthesis for the int() function call. Also you need to remove the + from print(+letter)

IndexError: list index out of range, for re.match

below is part of a script I wrote, in which I have a problem in the if statement.
If I want to use re.match('ATOM|MODEL',lines[i]), I got error message. removing the "|MODEL" in re.match, it will work.
can anyone give me some hints why this happens? Thank you very much!
new_pdb=open(pdb_out,'w')
i=0
while (i<len(lines)):
frag=lines[i].split()
# do not know why 'ATOM|MODEL' does not work
if (re.match('ATOM',lines[i]) and "H" not in frag[2]):
new_pdb.write(lines[i])
i=i+1
new_pdb.close()
Below is the error message when I used re.match('ATOM|MODEL',lines[i]):
Traceback (most recent call last):
File "non-h-1.py", line 17, in
if (re.match('ATOM|MODEL',lines[i]) and "H" not in frag[2]):
IndexError: list index out of range
At least one of the lines that starts with MODEL contains less than three whitespace-separated items, so frag[2] fails. If you remove |MODEL from the regex, re.match() fails and therefore Python doesn't even try to evaluate frag[2] which is why the error doesn't occur in that situation.
Other than that, you shouldn't be iterating over lines using a while loop - Python is not C. Use
for line in lines:
frag = line.split()
if (re.match('ATOM',line) and "H" not in frag[2]):
new_pdb.write(line)

Python dictionary SyntaxError

I'm moving to Python from PHP and I seem to be stuck in my first hour of learning Python.
This seems to be such a basic question that I'm having trouble finding an answer - so please forgive me.
When I try to create a dictionary I enter:
numbers = ('Bob':'322', 'Mary':'110', 'Joe':'839')
I get the error:
File "<stdin>", line 1
numbers = ('Bob':'322', 'Mary':'110', 'Joe':'839')
^
SyntaxError: invalid syntax
I've tried this in both the command line and in IDLE and the same error appears. What am I doing wrong? I really can't see it. Again sorry for low level of this question.
Dictionaries use curly braces {}, not parentheses ()
numbers = {'Bob':'322', 'Mary':'110', 'Joe':'839'}

Strange error about invalid syntax

I am getting invalid syntax error in my python script for this statement
44 f = open(filename, 'r')
45 return
return
^
SyntaxError: invalid syntax
I am not sure what exactly is wrong here? I am a python newbie and so will greatly appreciate if someone can please help.
I am using version 2.3.4
I had the same problem. Here was my code:
def gccontent(genomefile):
nbases = 0
totalbases = 0
GC = 0
for line in genomefile.xreadlines():
nbases += count(seq, 'N')
totalbases += len(line)
GC += count(line, 'G' or 'C')
gcpercent = (float(GC)/(totalbases - nbases)*100
return gcpercent
'return'was invalid syntax
I simply failed to close the bracket on the following code:
gcpercent = (float(GC)/(totalbases - nbases)*100
Hope this helps.
I got an "Invalid Syntax" on return when I forgot to close the bracket on my code.
elif year1==year2 and month1 != month2:
total_days = (30-day1)+(day2)+((month2-(month1+1))*30
return (total_days)
Invalid syntax on return.
((month2-(month1+1))*30 <---- there should be another bracket
((month2-(month1+1)))*30
Now my code works.
They should improve python to tell you if you forgot to close your brackets instead of having an "invalid" syntax on return.
Getting "invalid syntax" on a plain return statement is pretty much impossible. If you use it outside of a function, you get 'return' outside function, if you have the wrong indentation you get IndentationError, etc.
The only way I can get a SyntaxError: invalid syntax on a return statement, is if in fact it doesn't say return at all, but if it contains non-ascii characters, such as retürn. That give this error. Now, how can you have that error without seeing it? Again, the only idea I can come up with is that you in fact have indentation, but that this indentation is not spaces or tabs. You can for example have somehow inserted a non-breaking space in your code.
Yes, this can happen. Yes, I have had that happen to me. Yes, you get SyntaxError: invalid syntax.
i just looked this up because i was having the same problem (invalid syntax error on plan return statement), and i am extremely new at python (first month) so i have no idea what i'm doing most of the time.
well i found my error, i had forgotten an ending parentheses on the previous line. try checking the end of the previous line for a forgotten parentheses or quote?
>>> 45 return
File "<stdin>", line 1
45 return
^
SyntaxError: invalid syntax
>>>
That might explain it. It doesn't explain the 44 f = open(filename, 'r'), but I suspect that someone copied and pasted 45 lines of code where the indentation was lost and line numbers included.
Usually it's a parenthetical syntax error. Check around the error.
I encountered a similar problem by trying to return a simple variable assignation within an "if" block.
elif len(available_spots)==0:
return score=0
That would give me a syntax error. I fixed it simply by adding a statement before the return
elif len(available_spots)==0:
score=0
return score
I had the same problem. The issue in my case was, that i mixed up datatypes in my return statement. E.g.
return string + list + string

Python Syntax Error but looks fine to me. Help?

Right now I'm working on a Tetris Game (sorta, I found a Tetris example for Python on a website, I've been copying it but adding some of my own stuff), and just finished writing all the code but have had a couple syntax errors. I've been able to fix all of them but this last syntax error is confusing to me.
def pieceDropped(self):
for i in range(4):
x = self.curX + self.curPiece.x(i)
y = self.curY - self.curPiece.y(i)
self.setShapeAt(x, y, self.curPiece.shape()
self.removeFullLines()
The specific syntax error is on the last line of the function and I don't understand why, the indentation and whitespace all seems correct. So could someone explain how this is a syntax error?
You didn't close the parenthesis of self.setShapeAt.
There's an extra whitespace on the last line - just ahead self.removeFullLines(). Its indenting is thus not the same as the for line's indenting. EDIT: Seems to be corrected now.
Always use the same indent sequence - choose either tabs, or n whitespaces. But be consistent. Some editors (such as VIM) are able to insert the appropriate number of whitespaces whenever you hit tab.

Categories

Resources