我试图通过有限的成功随机顺序询问测验中的问题

时间:2016-02-12 13:04:34

标签: python

print("Transferring you to the quiz...")
print("Rules: ")
print("This quiz features questions about online safety. The questions are multiple choice so enter the number which corresponds to your answer. For every question you get right you will get a point. Good luck!")

score = 0
goes = 0

questions = ['0', '1', '2', '3', '4']

import random
from random import shuffle
random.shuffle(questions)

print(questions)


if questions == 0:
    questions.remove(0)

q1 = input("What is CEOP? 1.Child Exploitation and Online Protection 2.Criminal Exploration and Online Protection 3.Child Exploitation and Organised Protectors: ")
if q1 == "1":
    print("That's right, well done!")
    score = score + 1
    goes = goes + 1
else:   
    print("That's wrong, unlucky!")
    goes = goes + 1

elif questions == 1:
    questions.remove(1)
    q2 = input("When you get an email from someone you do not know, what should you do? 1.Reply and say hello 2.Forward to your friends 3.Delete it and mark as spam: ")
    if q2 == "3":
        print("That's right, well done!")
        score = score + 1
        goes = goes + 1
    else:
        print("That's wrong, unlucky!")
        goes = goes + 1

elif questions == 2:
    questions.remove(2)
    q3 = input("How secret should you keep your passwords? 1.Give them only to your best friends 2.Never give out passwords except to your parents 3.Give them to strangers: ")
if q3 == "2":
    print("That's right, well done!")
    score = score + 1
    goes = goes + 1
else:
    print("That's wrong, unlucky!")
    goes = goes + 1

elif questions == 3:
    questions.remove(3)
    q4 = input("When an online contact who frightens you asks to meet you in person what should you do? 1.Arrange to meet them 2.Arrange to meet them with your best friend 3.Report to CEOP: ")
if q4 == "3":
    print("That's right, well done!")
    score = score + 1
    goes = goes + 1
else:
   print("That's wrong, unlucky!")
   goes = goes + 1

    elif questions == 4:
    questions.remove(4)
q5 = input("If an email asks you to enter your bank account details because of a problem with your account what should you do? 1.Contact the bank to check if they sent the email 2.Reply to the email 3.Enter your bank account details: ")
if q5 == "1":
print("That's right, well done!")
    score = score + 1
    goes = goes + 1
else:
    print("That's wrong, unlucky!")
    goes = goes + 1

while goes == 5:        
    print("End of quiz")
    print("Well done, your score was {0}".format(score))
    break

我试图以随机顺序询问测验的问题。我已经洗了问题,但在打印随机列表后,测验停止了...... 我知道一些缩进不正确但它在我的代码中......

2 个答案:

答案 0 :(得分:1)

你应该在这里使用字典。字典已经包含随机顺序的元素。

dict = {"question1": "ans1", "question2": "ans2", "question3": "ans3"}
for s in dict.keys():
  ans = raw_input(s)
  if ans == dict[s]:
    print "success"
  else:
    print "failure"

答案 1 :(得分:0)

我会做一个包含元组的列表:

random.shuffle(list)

然后我会洗牌:

for i in list:
    answer = input(i[0])
    if answer == i[1]:
        print "succes"

最后,我会迭代它:

{{1}}

让我知道这是否有助于或将问题标记为已解决;)