Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: unexpected EOF while parsing

Tags:

I have no idea why this does not work please help

import random x = 0 z = input('?') int(z)  def main():     while x < z:         n1 = random.randrange(1,3)         n2 = random.randrange(1,3)         t1 = n1+n2         print('{0}+{1}={2}'.format(n1,n2,t1) 

When i run this it outputs this error

File "/Users/macbook/Documents/workspace/gamlir_filar/samlagning.py", line 12                                                  ^ SyntaxError: unexpected EOF while parsing 

I am using eclipse and python 3.3 and i have no idea why this happens. It sometimes outputs errors like this.

like image 801
Quar Avatar asked May 01 '13 22:05

Quar


People also ask

How do I fix EOF error in Python?

BaseException -> Exception -> EOFError The best practice to avoid EOF in python while coding on any platform is to catch the exception, and we don't need to perform any action so, we just pass the exception using the keyword “pass” in the “except” block.

What is Syntaxerror unexpected EOF while parsing in Python?

EOF stands for End of File in Python. Unexpected EOF implies that the interpreter has reached the end of our program before executing all the code. This error is likely to occur when: we fail to declare a statement for loop ( while / for ) we omit the closing parenthesis or curly bracket in a block of code.

What is EOFError EOF when reading a line?

Leave the input text box empty. Then, press Run test. You should get an error like EOFError: EOF when reading a line. The acronym EOF stands for End Of File.


2 Answers

You're missing a closing parenthesis ) in print():

print('{0}+{1}={2}'.format(n1,n2,t1)) 

and you're also not storing the returned value from int(), so z is still a string.

z = input('?') z = int(z) 

or simply:

z = int(input('?')) 
like image 186
Ashwini Chaudhary Avatar answered Sep 25 '22 10:09

Ashwini Chaudhary


Maybe this is what you mean to do:

import random  x = 0 z = input('Please Enter an integer: ') z = int(z) # you need to capture the result of the expressioin: int(z) and assign it backk to z  def main():     for i in range(x,z):         n1 = random.randrange(1,3)         n2 = random.randrange(1,3)         t1 = n1+n2         print('{0}+{1}={2}'.format(n1,n2,t1))  main() 
  1. do z = int(z)
  2. Add the missing closing parenthesis on the last line of code in your listing.
  3. And have a for-loop that will iterate from x to z-1

Here's a link on the range() function: http://docs.python.org/release/1.5.1p1/tut/range.html

like image 22
Kaydell Avatar answered Sep 21 '22 10:09

Kaydell