基于Python文本的随机问题测验

时间:2017-09-25 13:50:12

标签: python-3.4

这是一个基于文本的测验示例,其中程序应该为用户提供5个问题,以便以随机顺序回答。问题是它完美运行,但只提供3个随机问题,然后停止。

<div class="container">
  <div class="row">  
      <div><h2>Title 1</h2></div>
      <div><p>Test text 1</p></div>
      <div><p>Test text 2</p></div>
      <div><button>Button</button></div>

      <div><h2>Title 2</h2></div>
      <div><p>Test text 1 is a bit longer to force it to wrap</p></div>
      <div><p>Test text 2</p></div>
      <div><button>Button</button></div>

      <div><h2>Title 3 is a bit longer to make it wrap</h2></div>
      <div><p>Test text 1 is a bit longer to force it to wrap</p></div>
      <div><p>Test text 2</p></div>
      <div><button>Button</button></div>
        
      <div><h2>Title 4</h2></div>
      <div><p>Test text 1</p></div>
      <div><p>Test text 2 is a bit longer to force it to wrap</p></div>
      <div><button>Button</button></div>
  </div>
  
  <div class="row">  
      <div><h2>Title 5</h2></div>
      <div><p>Test text 1</p></div>
      <div><p>Test text 2</p></div>
      <div><button>Button</button></div>

      <div><h2>Title 6</h2></div>
      <div><p>Test text 1 is a bit longer to force it to wrap</p></div>
      <div><p>Test text 2</p></div>
      <div><button>Button</button></div>

      <div><h2>Title 7 is a bit longer to make it wrap</h2></div>
      <div><p>Test text 1 is a bit longer to force it to wrap</p></div>
      <div><p>Test text 2</p></div>
      <div><button>Button</button></div>
        
      <div><h2>Title 8</h2></div>
      <div><p>Test text 1</p></div>
      <div><p>Test text 2 is a bit longer to force it to wrap</p></div>
      <div><button>Button</button></div>
  </div>  
</div>

2 个答案:

答案 0 :(得分:1)

问题1,3和5是相同的,因此您只有三个独特的问题,因此只显示三个问题。只要random_question in used_questions的一个元素等于used_questionsrandom_question就会计算为True;他们不必在内存中引用完全相同的列表。

正如评论和其他答案中所提到的,在这里改组清单是一种更简单的方法。

import random

questions=[
    ["The answer is A","A","a"],
    ["The answer is B","B","b"],
    ["The answer is A","A","a"],
    ["The answer is F","F","f"],
    ["The answer is A","A","a"]
]

random.shuffle(questions)
for question in questions:
    print(question[0])
    players_answer=input("")
    if players_answer in question:
        print("\nCorrect!")
    else:
       print("\nWrong!")

结果:

The answer is F
F

Correct!
The answer is A
B

Wrong!
The answer is A
Q

Wrong!
The answer is B
B

Correct!
The answer is A
A

Correct!

答案 1 :(得分:0)

更简单的方法是使用shuffle

random.shuffle(questions)

这会使你的问题彻底改变。然后,您可以循环questions