2016-02-12 30 views
-2
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 

我想要以随机顺序询问测验的问题。我已经打乱了问题,但打印随机列表中的测验停止...... 后,我认识的一些缩进是不正确的,但它是在我的代码...我试图在有限成功的随机顺序中询问问题

+0

行'如果问题== 0: questions.remove(0)'将会给你的问题,因为你是问,如果一个列表等于一个整数。事实上,你所有的布尔测试用'if questions == '都会失败...... – isosceleswheel

+0

你应该做的是问'如果问题[0] ==“0”:remove(“0” )'因为你使用字符串作为随机的标识符列表,而不是整数。 – isosceleswheel

回答

0

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

list= [("myQuestion","answer"),("question2", "answer2")] 

我会那么将它洗:

random.shuffle(list) 

最后,我会遍历它:

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

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

1

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

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"