因此,我正在尝试进行多项选择测试(目前仍处于测试阶段),Pycharm没有看到定义好的变量/字符串。它可以在codeskulptor.org中工作,但不能在Pycharm中工作。我甚至在我的mac电脑上尝试过终端,但仍然没有成功。它在NameError中给出了相同的错误“文件”,第1行:名称'a‘未定义“。有人能帮我解决这个问题吗?"a在"\n(a)很小,很简单,没有核,“这就是问题的答案,问题在”问题(question_prompts,"a")“,我想是因为它就是在那里定义的,对吧?
import time
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
question_prompts = [
"""prokaryotic cells have what characteristics?\n(a) are small and simple, lacks a nucleus,
do not have organells, are a single cell creatures, and have a cell wall.\n\n """
]
questions = [
Question(question_prompts[0], "a")
]
print("Cell Unit Test")
time.sleep(2)
print("10 seconds until test time, take your stuff off your desk")
#time.sleep(10)
print("The test will now commence")
time.sleep(1)
def run_test(questions):
score = 0
for question in questions:
print(question.prompt)
answer = input("Answer:\n ")
if answer == question.answer:
print("Correct!\n")
score += 1
else:
print("Wrong! The correct answer is " + question.answer + "\n")
print("\nEnd of the test.")
print("You got " + str(score) + " out of " + str(len(questions)) + " questions correct")
run_test(questions)
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
question_prompts = [
"""prokaryotic cells have what characteristics?\n(a) are small and simple, lacks a nucleus,
do not have organelles, are single-cell creatures, and have a cell wall.\n\n """
]
questions = [
Question(question_prompts[0], "a")
]
print("Cell Unit Test")
time.sleep(2)
print("10 seconds until test time, take your stuff off your desk")
#time.sleep(10)
print("The test will now commence")
time.sleep(1)
def run_test(questions):
score = 0
for question in questions:
print(question.prompt)
answer = input("Answer:\n ")
if answer == question.answer:
print("Correct!\n")
score += 1
else:
print("Wrong! The correct answer is " + question.answer + "\n")
print("\nEnd of the test.")
print("You got " + str(score) + " out of " + str(len(questions)) + " questions correct")
run_test(questions)'发布于 2019-11-22 09:36:35
这是Python3代码,但是您正在使用Pycharm中的Python2解释器来解释它。之所以会出现这个错误,是因为Python2中的input具有“类似eval”的行为,并且它试图从字面上将您输入的"a“解释为要引用的变量。
将Pycharm中使用的解释器更改为Python 3解释器。或者,您也可以将input更改为raw_input,但通过使用print()来判断,这是为了在Python3解释器中运行。
https://stackoverflow.com/questions/58986129
复制相似问题