“开放式”问题,在python中带来了微不足道的挑战

时间:2019-03-07 16:04:32

标签: python python-3.x

我想提出一个琐碎的挑战!但这没有像我那样工作。这是我的代码:

import os
with open('questions','r') as file:
   l = 0
   question = []
   right_guess = []
   wrong_guess_1 = []
   wrong_guess_2 = []
   for line in file:
        content_line = []
        for char in line:
             content_line.append(char)
        if l == 0:
             question.append(content_line)
             l = 1
        elif l == 1:
             right_guess.append(content_line)
             l = 2
        elif l == 2:
             wrong_guess_1.append(content_line)
             l = 3
        elif l == 3:
             wrong_guess_2.append(content_line)
             l = 4
   if l == 4:
        break
   print(question)
   print(right_guess)
   print(wrong_guess_1)
   print(wrong_guess_2)
   input('Awnser : ')

好,现在让我解释两个问题。我已经尝试使用此问题文件结构(没有扩展,只是'question')编写代码:

 a question
      the right anwser
      false guess
      false guess
 a other question
      the right anwser
      false guess
      false guess

如果您不了解,请参见以下示例:

 How does 1 + 1 ?
  3
  0
  5

显然,答案是第二行:3! (开玩笑的是2,我知道:D) 因此,让我们回到问题所在:我已经启动了代码,并且得到了这个结果:

 [[ 'H', 'o', 'w', ' ', 'd', 'o', 'e', 's', ' ', '1', '+', '1', ' ', '?', '\n']]
 [[ 'H', 'o', 'w', ' ', 'd', 'o', 'e', 's', ' ', '1', '+', '1', ' ', '?', '\n']]
 [[ 'H', 'o', 'w', ' ', 'd', 'o', 'e', 's', ' ', '1', '+', '1', ' ', '?', '\n']]
 [[ 'H', 'o', 'w', ' ', 'd', 'o', 'e', 's', ' ', '1', '+', '1', ' ', '?', '\n']]
 Answer :

我不太清楚问题出在哪里,但我希望有人能帮助我获得此结果:

 How does 1+1 ?
 2
 0
 5
 Answer

如果某些会观看此页面的人真的很疯狂,我可以预期会有这样的结果:

 (the program take one question, randomly, in the whole file)
 the chosen one
 one of the three possibles guests
 one other of the three possibles guests
 a third possible guest
 ( And we don't know which one is the correct )
 Answer :

我想这就是全部,感谢所有尝试提供线索,元素或将为这个小程序做出贡献的人!

3 个答案:

答案 0 :(得分:2)

您的第一个问题在这里:

content_line = []
for char in line:
    content_line.append(char)

这些都不是必需的。接下来的几行只需使用line

for line in file:
    if l == 0:
        question.append(line)

答案 1 :(得分:1)

问题中的代码多次未将同一行输出。如果这是一个问题,则此代码中不会显示它。


获取列表而不是字符串的原因是,您明确地制作了这些列表:

        content_line = []
        for char in line:
             content_line.append(char)

如果您直接使用line(或者如果要在末尾删除换行符,则直接使用line.rstrip()),则文件中的每一行都会得到一行文本。


获取列表列表内部的原因是,您不仅要在列表中放置字符,还要将这些字符附加到另一个列表中。似乎每个列表中只有一个项目,而且您希望从列表中打印出一个字符串,因此只需将这些列表替换为字符串即可。

无需附加列表,只需分配变量即可:

   for line in file:
        # Remove trailing whitespace at the end of the line
        content_line = line.rstrip()
        if l == 0:
             question = content_line
             l = 1
        elif l == 1:
             right_guess = content_line
             l = 2
        elif l == 2:
             wrong_guess_1 = content_line
             l = 3
        elif l == 3:
             wrong_guess_2 = content_line
             l = 4

答案 2 :(得分:0)

好的,所以这看起来是最终的代码:

for line in file:
    # Remove trailing whitespace at the end of the line
    content_line = line.rstrip()
    if l == 0:
         question = content_line
         l = 1
    elif l == 1:
         right_guess = content_line
         l = 2
    elif l == 2:
         wrong_guess_1 = content_line
         l = 3
    elif l == 3:
         wrong_guess_2 = content_line
         l = 4

感谢quamrana,user1018651和任何发表评论以帮助我的人! 无论如何,这仍然是一件“小”事情,我认为这是最难的事情: 转换此代码以添加随机问题,最后得到:

  (the program take one question, randomly, in the whole file)
  the chosen one
  one of the three possibles guests
  one other of the three possibles guests
  a third possible guest
  ( And we don't know which one is the correct )
  Answer :