首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pycharm看不到定义好的东西

Pycharm看不到定义好的东西
EN

Stack Overflow用户
提问于 2019-11-22 09:22:54
回答 1查看 105关注 0票数 0

因此,我正在尝试进行多项选择测试(目前仍处于测试阶段),Pycharm没有看到定义好的变量/字符串。它可以在codeskulptor.org中工作,但不能在Pycharm中工作。我甚至在我的mac电脑上尝试过终端,但仍然没有成功。它在NameError中给出了相同的错误“文件”,第1行:名称'a‘未定义“。有人能帮我解决这个问题吗?"a在"\n(a)很小,很简单,没有核,“这就是问题的答案,问题在”问题(question_prompts,"a")“,我想是因为它就是在那里定义的,对吧?

代码语言:javascript
复制
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)'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-22 09:36:35

这是Python3代码,但是您正在使用Pycharm中的Python2解释器来解释它。之所以会出现这个错误,是因为Python2中的input具有“类似eval”的行为,并且它试图从字面上将您输入的"a“解释为要引用的变量。

将Pycharm中使用的解释器更改为Python 3解释器。或者,您也可以将input更改为raw_input,但通过使用print()来判断,这是为了在Python3解释器中运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58986129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档