Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python_Python 3.x - Fatal编程技术网

如何剥离python

如何剥离python,python,python-3.x,Python,Python 3.x,我正在为学校写一个测验代码。代码遍历文本文件并从中加载问题和答案。用户将选择一个难度进行测验。答案选项的数量将根据难度的不同而有所不同。我已经在文本文件中用逗号将每个问题和可能的答案分开 from random import shuffle file = open("maths.txt" , "r") for line in file: question = line.split(",") print(question[0]) if difficulty in ("e"

我正在为学校写一个测验代码。代码遍历文本文件并从中加载问题和答案。用户将选择一个难度进行测验。答案选项的数量将根据难度的不同而有所不同。我已经在文本文件中用逗号将每个问题和可能的答案分开

from random import shuffle

file = open("maths.txt" , "r")
for line in file:
    question = line.split(",")
    print(question[0])
    if difficulty in ("e", "E"):
        options = (question[1], question[2])

    if difficulty in ("m", "M"):
        options = (question[1], question[2], question[3])

    if difficulty in("h", "H"):
        options = (question[1], question[2], question[3], question[4])

    options = list(options)
    shuffle(options)
    print(options)

    answer = input("Please enter answer: ")

    if answer in (question[1]):
        print("Correct!")
    else:
        print("incorrect")
  file.close()
这是一行文本文件的外观: 问题1。什么是4+5?、9、10、20、11


第一个选项问题[1]将始终是正确答案,因此我想重新排列选项。使用此代码,选项以方括号、换行符和引号输出。有人知道我怎么才能脱掉这些吗?我试着使用:line.split.strip,但这似乎毫无用处。谢谢

问题是您正在尝试打印列表对象。相反,您应该打印每个选项。您可能最好在其周围打印一些格式:

for option in options:
    print(option)
for option_num, option in enumerate(options):
    print("{} - {}").format(option_num, option)

请阅读有关enumerate and format的内容,以准确了解此处发生的情况。要从字符串中删除字符,请使用.rstripput text此处删除,从字符串右端删除字符;使用.lstriptext删除,从字符串左端删除字符。

类似的内容

from random import shuffle
def maths_questions():
  file = open("maths.txt" , "r")
  for line in file:
    question = line.strip().split(",") # every line in file contains newline. add str.strip() to remove it
    print(question[0])

    if difficulty in ("e","E"):
        options = [question[1],question[2]]
    elif difficulty in ("m","M"):
        options = [question[1],question[2],question[3]]
    elif difficulty in("h","H"):
        options = [question[1],question[2],question[3],question[4]]
    # why to create tuple and then convert to list? create list directly

    shuffle(options) #shuffle list
    print("Options: ", ", ".join(options)) # will print "Options: opt1, opt2, opt3" for M difficulty

    answer=input("Please enter answer: ")

    if answer in (question[1]):
            print("Correct!")
    else:
        print("Incorrect, please try again...")
  file.close()
:

可接合的

返回一个字符串,该字符串是iterable中字符串的串联。如果iterable中有任何非字符串值,包括bytes对象,则会引发TypeError。元素之间的分隔符是提供此方法的字符串


预期产量是多少?如果在m中遇到困难,也要更新这个,m:line.split.strip应该会引起错误,而不是什么都不做。Math.txt的内容是什么样子的?请将其添加到代码块中的问题中。您应该使用标准库中的来读取Math.txt,以便正确分割行。这将处理好换行符并正确处理答案文本中的逗号。我觉得问题标题很有趣。需要一个.stripsomewhere@Blurp我相信上面提到的逗号和括号实际上来自Python打印列表的方式,这就是为什么答案比看起来要简单得多的原因。不需要剥离最后一个选项不需要剥离新行吗?选项可能有前导空格和尾随空格。这就是为什么我建议使用csv模块来读取上面的文件。