Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 - Fatal编程技术网

Python 我试图让测验中的问题以随机顺序提问,但成功率有限

Python 我试图让测验中的问题以随机顺序提问,但成功率有限,python,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 num

我试图让测验的问题以随机顺序提问。我已经把问题混在一起了,但是在打印了随机列表之后,测验就停止了。。。
我知道有些缩进不正确,但它在我的代码中…

我会列出包含元组的列表:

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
然后我会将其洗牌:

list= [("myQuestion","answer"),("question2", "answer2")]
最后,我想重复一下:

random.shuffle(list)

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

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

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

如果questions==0:questions.remove(0)这几行会给您带来问题,因为您会询问列表是否等于整数。事实上,使用for
if questions==
的所有布尔测试都将失败……您应该做的是询问
if questions[0]==“0”:删除(“0”)
,因为您使用字符串作为随机标识符列表,而不是整数。
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"