psychopy.org | Reference | Downloads | Github

Nameerror - Coder View

I keep getting this error message, however, I’m not too sure why
########### Running: /Volumes/SARAH/psychopygonogo2_lastrun.py ############
2019-04-06 20:40:28.728 python[2665:184691] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
Traceback (most recent call last):
File “/Volumes/SARAH/psychopygonogo2_lastrun.py”, line 352, in
if resp.corr:#stored on last run routine
NameError: name ‘resp’ is not defined

Does anyone know what the issue might be?

Thank you!

if resp.corr:
msg=“Correct! RT=%.3f” %(resp.rt)
msgcolour= ‘blue’
#blue colour for correct answers
else:
msg=“Oops! That was wrong”
msgcolour= ‘red’
#red colour for wrong answers
corr.append(resp.corr)
nCorr = sum(corr[-5:])
nResps = len(corr[-5:])
msg += “({} out of {} correct)” .format (nCorr,nResps)

This is how I’ve coded it.

I’m afraid this isn’t enough information to tell exactly what’s going on, but in general terms, a NameError is what happens when Python doesn’t think a variable exists. So, what it’s saying is that you’re asking about resp.corr before you define what resp.corr is.

What you need to show us is where resp.corr is set initially.

Thank you for your response. That’s where I’m confused, as I haven’t written it in excel, only in the coding section, so I think the problem lies there.

![B40A003E-78F2-4860-94CF-987CBE6E3187|281x500]

@Sarah952, resp.corr looks like a reference to a keyboard component attribute to check whether a correct response was given. However, if you do not have a keyboard component called “resp”, this will give you a name error, because “resp” has not been defined as a keyboard object. Have you got a keyboard component called “resp” in your routine?

Yes, I do, do I need to change the start duration of the keyboard response?

Your screenshot shows a keyboard component called key_resp_2, not resp?

The issue is not the start time of the component. It is either:

  • no object of that name (resp) exists, or
  • it exists, but not at the time that the code that refers to is called.

At the moment we can’t tell from your screenshots where the code component is called (i.e. on which routine it runs), or whether a component called resp exists on the feedback routine, so it is a bit hard to give precise guidance.

PS your Mac can take screenshots directly for you, which is easier than mucking about with a phone:

I’ve actually run into the same issue and even made sure my keyboard component name matches the code in the code component, but I still run into the same error as @Sarah952.

What else could I be doing wrong?

We need more details (i.e. show us your code and your error message), but also note:

Thank you for your response, I’ll take a look at it again and see if I can resolve the issue.