22

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.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Quar
  • 369
  • 3
  • 4
  • 10
  • 14
    Whenever you get a `SyntaxError` that makes no sense on a line that looks perfectly good, or completely empty, it's worth looking at the line before it. If the previous line has unclosed parentheses, brackets, braces, etc., then the line you're looking at is treated as a continuation of the previous line. – abarnert May 01 '13 at 22:23
  • A couple side notes: Just calling `int(z)` doesn't do anything (except raise an exception is `z` can't be parsed as an integer); you need to store the result somewhere (e.g., `z = int(z)`). Also, because you never modify `x` or `z` inside the loop (and if you _did_, it would give you an `UnboundLocalError`), `x < z` will never change, so once you get into the loop, you can never get out again. – abarnert May 01 '13 at 22:26

2 Answers2

37

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('?'))
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

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

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Kaydell
  • 484
  • 1
  • 4
  • 14