Python NameError: name is not defined for my script

Python NameError: name is not defined for my script

我有一个 python 脚本,但我收到以下错误。我是这门语言的新手,所以我创建了一个名为 writing.py 的简单脚本,用于将参与者姓名和分数写入名为 scores.txt 的文本文件中。但我不断收到此错误:

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

这是我的代码:

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()

您正在使用 Python 2。在 Python 2 中,input takes your input and tries to evaluate it. You want to use raw_input

我猜你正在使用 Python 2.x ,在 Python 2.x 中, input 实际上试图在返回结果之前评估输入,因此如果你输入一些名称,它会将其视为一个变量并尝试获取导致问题的值。

使用raw_input()。反而。例子-

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