Repository: psychopy/psychopy

Issue:
Invalid literal for int() when assigning variable to "N Vertices"
I'm trying to tell psychopy to display a polygon with a random number of vertices on each presentation by using randint.  I keep running into this error:

`invalid literal for int() with base 10: 'randint(3, 4, 1)'`

This occurs before the script compiles, so it doesn't produce any output / traceback other than that.  

I've also tried this using $randint(3, 4, 1) but that doesn't work as well.  In addition, my friend had this problem when specifying the number of angles via an excel file.

When I just insert a number to the field, it works fine, so it seems like python is trying to interpret randint(3, 4, 1) literally, sees that it isn't an integer, and throws an error.

Variable assignment:
![image](https://f.cloud.github.com/assets/1839645/1603669/14c4adc4-53ae-11e3-9c09-4e562ed69e8c.png)

Error after clicking run:
![image](https://f.cloud.github.com/assets/1839645/1603672/27c7b952-53ae-11e3-8a0a-4402f6da4fb7.png)



Discussion:
I suspect that this is due to the text in .val not being properly eval()ed as code, and int("randint(3,4,1)") causes the error.

when setting N vertices to update every repeat I also get an: "I don't know the appropriate default value for a 'nVertices' parameter. Please email the mailing list about this error"

The issue is not easy to fix. To enable dynamic control over the number of sides, the polygon component writeInitCode() method would need to eval() the user's code expression. This will work for things like '3', but will often fail for things like randint(3,4,). Basically, code is intended to be eval()d at run time, not compile time. Things like randint() and user-defined vars will not be in the namespace at compile time, but will be at run time. 


Resolving patch:
diff --git a/psychopy/app/builder/components/polygon.py b/psychopy/app/builder/components/polygon.py
--- a/psychopy/app/builder/components/polygon.py
+++ b/psychopy/app/builder/components/polygon.py
@@ -32,8 +32,8 @@ def __init__(self, exp, parentName, name='polygon', interpolate='linear',
         self.exp.requirePsychopyLibs(['visual'])
         self.order=['nVertices']
         #params
-        self.params['nVertices']=Param(nVertices, valType='code',
-            updates='constant', allowedUpdates=['constant','set every repeat'],
+        self.params['nVertices']=Param(nVertices, valType='int',
+            updates='constant', allowedUpdates=['constant'],
             hint="How many vertices? 2=line, 3=triangle... (90 approximates a circle)",
             label="N Vertices")
         self.params['fillColorSpace']=Param(fillColorSpace, valType='str', allowedVals=['rgb','dkl','lms'],
@@ -72,18 +72,19 @@ def writeInitCode(self,buff):
         if self.params['units'].val=='from exp settings': unitsStr=""
         else: unitsStr="units=%(units)s, " %self.params
         inits = getInitVals(self.params)#replaces variable params with defaults
-        if int(self.params['nVertices'].val) == 2:
+        if self.params['nVertices'].val == '2':
             buff.writeIndented("%s = visual.Line(win=win, name='%s',%s\n" %(inits['name'],inits['name'],unitsStr))
             buff.writeIndented("    start=(-%(size)s[0]/2.0, 0), end=(+%(size)s[0]/2.0, 0),\n" %(inits) )
-        elif int(self.params['nVertices'].val) == 3:
+        elif self.params['nVertices'].val == '3':
             buff.writeIndented("%s = visual.ShapeStim(win=win, name='%s',%s\n" %(inits['name'],inits['name'],unitsStr))
             buff.writeIndented("    vertices = [[-%(size)s[0]/2.0,-%(size)s[1]/2.0], [+%(size)s[0]/2.0,-%(size)s[1]/2.0], [0,%(size)s[1]/2.0]],\n" %(inits) )
-        elif int(self.params['nVertices'].val) == 4:
+        elif self.params['nVertices'].val == '4':
             buff.writeIndented("%s = visual.Rect(win=win, name='%s',%s\n" %(inits['name'],inits['name'],unitsStr))
             buff.writeIndented("    width=%(size)s[0], height=%(size)s[1],\n" %(inits) )
         else:
             buff.writeIndented("%s = visual.Polygon(win=win, name='%s',%s\n" %(inits['name'],inits['name'],unitsStr))
-            buff.writeIndented("    edges = %(nVertices)s, size=%(size)s,\n" %(inits) )
+            buff.writeIndented("    edges = %s," % str(inits['nVertices'].val))
+            buff.writeIndented(" size=%(size)s,\n" %(inits) )
         buff.writeIndented("    ori=%(ori)s, pos=%(pos)s,\n" %(inits) )
         buff.writeIndented("    lineWidth=%(lineWidth)s, lineColor=%(lineColor)s, lineColorSpace=%(lineColorSpace)s,\n" %(inits) )
         buff.writeIndented("    fillColor=%(fillColor)s, fillColorSpace=%(fillColorSpace)s,\n" %(inits) )


Test patch: