1

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()
1
  • The name Helen is just the user input. It applies to any participant name I add. Commented Jul 6, 2015 at 17:46

2 Answers 2

3

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 + "> ")
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.