2017-02-23 75 views
-3

任何人都可以帮助我,我已经发布了我的整个工作任何人都可以告诉我如何洗牌这五个问题,请让我非常感谢。如何在python中洗牌问题

print("welcome to the quiz") 
Validation = False 

while Validation ==False: 

    name=input("Please enter your name") 
    age=int(input("Please enter your age")) 
    print("Use keboard to play the quiz") 
    play=input("Are you ready for the online safety questions") 
    if play.capitalize()== "Yes": 
     if age >16: 
      print("This quiz might be easier for you") 

     print("welcome to the quiz") 
     Validation = False) 

     question1=input("Q1:What is CEOP?/A:Child Exploitation and Online Protection/B:Child Exploitation and Organised Protectors/C:Criminal Exploration and Online Protection") 
     score = 0 
     if question1.capitalize() == "A": 
      print("WELL DONE") 
      score = score + 5 
     elif question1.capitalize() =="B": 
      print("Wrong Answer") 
     elif question1.capitalize() =="C": 
      print("Wrong Answer") 
     else: 
      print("Wrong Answer") 
     print("so far, you have",score,"marks") 

     question2=input("Q2:When you get an email from someone you do not know, what should you do?/A:Reply and say hello/B:Delete it and mark as spam/C:Forward to your friends") 
     if question2.capitalize() == "B": 
      print("WELL DONE") 
      score = score + 10 
     elif question2.capitalize() =="A": 
      print("Wrong Answer") 
     elif question2.capitalize() =="C": 
      print("Wrong Answer") 
     else: 
      print("Wrong Answer") 
     print("so far, you have",score,"marks") 

     question3=input("Q3:How secret should you keep your passwords?/A:Give them to strangers/B:Give them only to your best friends/C:Never give out passwords except to your parents") 
     if question3.capitalize() == "C": 
      print("WELL DONE") 
      score = score + 15 
     elif question3.capitalize() =="B": 
      print("Wrong Answer") 
     elif question3.capitalize() =="A": 
      print("Wrong Answer") 
     else: 
      print("Wrong Answer") 
     print("so far, you have",score,"marks") 

     question4=input("Q4:When an online contact who frightens you asks to meet you in person what should you do?/A:Arrange to meet them with your best friend/B:Arrange to meet them/C:Report to CEOP") 
     if question4.capitalize() == "C": 
      print("WELL DONE") 
      score = score + 20 
     elif question4.capitalize() =="B": 
      print("Wrong Answer") 
     elif question4.capitalize() =="A": 
      print("Wrong Answer") 
     else: 
      print("Wrong Answer") 
     print("so far, you have",score,"marks") 

     question5=input("Q5:If an email asks you to enter your bank account details because of a problem with your account what should you do?/A:Reply to the email/B:Contact the bank to check if they sent the email/C:Enter your bank account details") 
     if question5.capitalize() == "B": 
      print("WELL DONE") 
      score = score + 25 
     elif question5.capitalize() =="C": 
      print("Wrong Answer") 
     elif question5.capitalize() =="A": 
      print("Wrong Answer") 
     else: 
      print("Wrong Answer")  

     print ("Thank you for doing the quiz", name,".You scored", score, "marks in the quiz") 
     Validator=True 
    else: 
     Validation=False 

回答

4

你不能以硬编码的方式对它们进行洗牌。将问题存储在列表中,并使用该列表上的random.shuffle函数。你可以这样做

validation = False 
while validation ==False: 
    # input age 
    if age > 16: 
     validation = False 

    questions = [ 
     ('question 1 text', 'A'), 
     ('question 2 text', 'C')...] 

    random.shuffle(questions) 

    for question_text, correct_answer in questions: 
     user_input = input(question_text) 

     if user_input.capitalize() == correct_answer: 
      score += 5 
      print("well done") 
     else: 
      print("wrong answer") 

    validation = True 
+0

你可以给我一个例子吗? –

+0

@HurAli:编辑 –

+0

仍然困惑,因为我们粗略的工作,我们需要使用while循环或函数来做到这一点,也需要使用验证。 –

0

首先请与您的问题和答案的列表(或字典):

Q = [ 
    "What is CEOP?", #Each question and answer is defined by a number this one is 0 
    "When you get an email from someone you do not know, what should you do?", #This one is 1 
    "How secret should you keep your passwords?", # 2 
    "When an online contact who frightens you asks to meet you in person what should you do?", #And so on 
    "If an email asks you to enter your bank account details because of a problem with your account what should you do?" 
    ] 

A = [ 
    "A:Child Exploitation and Online Protection/B:Child Exploitation and Organised Protectors/C:Criminal Exploration and Online Protection", 
    "A:Reply and say hello/B:Delete it and mark as spam/C:Forward to your friends", 
    "A:Give them to strangers/B:Give them only to your best friends/C:Never give out passwords except to your parents", 
    "A:Arrange to meet them with your best friend/B:Arrange to meet them/C:Report to CEOP", 
    "A:Reply to the email/B:Contact the bank to check if they sent the email/C:Enter your bank account details", 
    ] 

定义列表可跟踪的显示问题:

QAsked = [] 

做出选择问题的定义:

def SearchQ(): 
QProposed = random.randint(0,4) 
    if QProposed in QAsked: #If already asked 
     SearchQ()   #Search again 
    else: 
     QAsked.append(QProposed) 
     print(Question: Q[QProposed]) 
     print(Answer: A[QProposed]) 
     CheckIfOK(QProposed) 

请记住from random import randint

CheckIfOK(Question)是一样的东西:

if Question == 1: 
    ans = input("Answer: ") 
    if ans.capitalize() == "A": 
     print("WELL DONE") 
     score = score + 25 
    elif ans.capitalize() =="B": 
     print("Wrong Answer") 
    elif ans.capitalize() =="C": 
     print("Wrong Answer") 
    else: 
     print("That's not an answer") 
if Question == 2: 
    ans = input("Answer: ") 
    if ans.capitalize() == "C": 
     print("WELL DONE") 
     score = score + 25 
    elif ans.capitalize() =="B": 
     print("Wrong Answer") 
    elif ans.capitalize() =="A": 
     print("Wrong Answer") 
    else: 
     print("That's not an answer") 
...