因此,我正在尝试进行多项选择测试(目前仍处于beta测试中,Pycharm并未看到所定义的变量/字符串。它可以在codeskulptor.org中工作,但不能在Pycharm中工作。我什至在Mac上尝试通过Terminal仍然没有运气。它在NameError中给我同样的错误“ File”,第1行:未定义名称“ a”。有人可以帮我解决这个问题吗? “ a位于“ \ n(a)小而简单,缺少原子核,”将是问题的答案。问题在于“ Question(question(question_prompts [0],“ 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)'
因此,我正在尝试进行多项选择测试(目前仍处于beta测试中,Pycharm并未看到所定义的变量/字符串。它可以在codeskulptor.org中工作,但不能在Pycharm中工作。我什至尝试了...
这是Python 3代码,但是您正在Pycharm中使用Python 2解释器对其进行解释。之所以会出现此错误,是因为Python 2中的input
具有“类似eval”的行为,并且它试图从字面上解释将您输入的“ a”作为要引用的变量。]>
[将您在Pycharm中使用的解释器更改为Python 3解释器。或者,您也可以将input
更改为raw_input
,但是通过使用print()
判断,该操作旨在在Python 3解释器中运行。