Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将str.partition与文本文件结合使用来分割问答题_Python_Python 3.x - Fatal编程技术网

Python 如何将str.partition与文本文件结合使用来分割问答题

Python 如何将str.partition与文本文件结合使用来分割问答题,python,python-3.x,Python,Python 3.x,我正在尝试做一个使用外部文本文件的测验。文本文件如下所示: 苹果是绿色的吗?是的 梨是绿色的吗?是的 等等 我使用了x.partition?[0]在问号之间分割,因此问题在左边,答案在右边。 然而,当我运行程序时,答案似乎与CSR答案不匹配,我不知道为什么。 我尝试了csAnswer.rstrip,但它仍然输出“不正确” 我怎样才能修改这一点 问题: 对于文件中的x: printx.分区?[0] 答案=输入答案,正确或错误: csAnswer=x.分区?[2] csAnswer.rstrip\n

我正在尝试做一个使用外部文本文件的测验。文本文件如下所示:

苹果是绿色的吗?是的 梨是绿色的吗?是的 等等 我使用了x.partition?[0]在问号之间分割,因此问题在左边,答案在右边。 然而,当我运行程序时,答案似乎与CSR答案不匹配,我不知道为什么。 我尝试了csAnswer.rstrip,但它仍然输出“不正确”

我怎样才能修改这一点

问题: 对于文件中的x: printx.分区?[0] 答案=输入答案,正确或错误: csAnswer=x.分区?[2] csAnswer.rstrip\n printcs is,csAnswer,answer输入为,answer 如果答案==csAnswer: 打印正确答案 伊里夫回答=答复: 打印错误 尝试如下使用。剥离并分配给变量:

def csQuestions():
    for x in questionFile:
        que,ans = x.split("?")
        print(que)
        answer = input("Input answer, TRUE OR FALSE: ")
        csAnswer = ans
        csAnswer = csAnswer.strip()
        print("cs is ",csAnswer,"answer input is ",answer)
        if answer == csAnswer:
            print("correct answer")
        elif answer != csAnswer:
            print("incorrect")

老实说,我不会使用分区,我会修改文本文件,使其为Apple green?TRUE,以便我们可以使用如下拆分方法:

for questionandanswer in questionFile:
     question = questionandanswer.split('"')[0]
     answer = questionandanswer.split('"')[1]
在运行代码时,代码会与空格等混淆,现在这种情况不会再发生了。我使用它的ur代码重新命名,但修改为。拆分:

questionFile = open(r'C:\Users\incan\OneDrive\Documentos\Code\try.txt', 'r')
def csQuestions():
    for x in questionFile:
        print(x.split('"')[0])
        answer = input("Input answer, TRUE OR FALSE: ")
        csAnswer = x.split('"')[1]
        csAnswer.rstrip("\n")
        print("cs is ",csAnswer,"answer input is ",answer)
        if answer == csAnswer:
            print("correct answer")
        elif answer !=csAnswer:
            print("incorrect")

 csQuestions()



     

你试过打印csAnswer吗?是的,为了看看我写的printcs is,csAnswer,answer input is,answer的输出是什么?可能是csAnswer前面的一个空格-use.strip为什么要使用[2]来获取答案,为什么要对字符串进行两次分区?做一次,然后收集零件?@mkrieger1我想它会去除空间和不需要的东西,所以它对美沙酮有效!比我所做的要简单得多