merit2go
merit2go

Reputation: 13

Python NameError: name is not defined for my script

I have a python script and I am receiving the following error. I'm a new learner to this language, so I created a simple script, called writing.py, to write participant names and scores into a text file named scores.txt. But I keep getting this error:

Traceback (most recent call last):
  File "writing.py", line 4, in <module>
    participant = input("Participant name > ")
  File "<string>", line 1, in <module>
NameError: name 'Helen' is not defined

Here is my code:

f = open("scores.txt", "w")

    while True:
        participant = input("Participant name > ")

        if participant == "quit":
            print("Quitting...")
            break

    score = input("Score for " + participant + "> ")
    f.write(participant + "," + score + "\n")

f.close()

Upvotes: 1

Views: 2434

Answers (2)

Anand S Kumar
Anand S Kumar

Reputation: 90909

I am guessing you are using Python 2.x , in Python 2.x , input actually tries to evaluate the input before returning the result, hence if you put in some name , it will treat that as a variable and try to get its value causing the issue.

Use raw_input(). instead. Example -

participant = raw_input("Participant name > ")
....
score = raw_input("Score for " + participant + "> ")

Upvotes: 3

Morgan Thrapp
Morgan Thrapp

Reputation: 9986

You're using Python 2. In Python 2, input takes your input and tries to evaluate it. You want to use raw_input.

Upvotes: 1

Related Questions