-2

I have an error that looks like this once I input Chelsea into my Python script. I really can't see what is going wrong as I've used input functions before in Python and this has never happened before.

  File "<string>", line 1, in <module>
NameError: name 'Chelsea' is not defined

The code looks like this

teamDic = {'Chelsea' : 2}
a = input('Enter your name:')
print(teamDic[a])
Fred Cozzi
  • 535
  • 1
  • 6
  • 12

1 Answers1

1

I had this error for a while when I was making a program that searches a Premier League teams statistics. I'm not a beginner when it comes to programming but sometimes the little things can trip you up and you just can't see a way out. (Even though it can seem fairly obvious to others)

If you too are getting this error the first thing I say to you is CHECK YOUR VERSION OF PYTHON. Python 2.7 uses the raw_input() function whereas Python 3 versions use input().

If you are using Python 2.7 I recommend installing Python 3 and using "python3 <yourfile.py>" in the command line. This will then allow you to use the input() function.

It is also worth noting that Python stopped supporting 2.x back at the start of 2020 so there is no need to be using it still (just my opinion).

Fred Cozzi
  • 535
  • 1
  • 6
  • 12