0

What I try to do is randomise the question1 to question20 in different order and when it's randomise I can also type my answer in the question. Every time I try to use the randomise statement it's always stuff up with error or intend error. what the code do now is print the question in order. all I have to do is randomise the question with the if and elif statement together

{py}

name = raw_input("Hello what is your name? ")

print "Hi " + name

ready = raw_input("Are you ready to do the test?(Please enter YES/NO)")

while ready.lower() != "yes":
ready = raw_input("Are you ready to do the test?(Please enter YES/NO)")

if ready.lower() == "yes":
    print "Excelent! The test will start in any second. "
if ready.lower() == "no":
    print "Okay, tell me when you're ready. "
elif ready.lower() != "yes" and "no":
    print "It okay if your're not sure. The test can start in any time you want. "

count = 0


if ready.lower() == "yes":

question1 = raw_input("\nQuestion 1\nWhat is your a?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question1.lower() == "a":
    print "Correct"
    count = count + 1
elif question1.lower() != "a":
    print "Wrong"

question2 = raw_input("\nQuestion 2\nWhat is your b?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question2.lower() == "a":
    print "Correct"
    count = count + 1
elif question2.lower() != "a":
    print "Wrong"

question3 = raw_input("\nQuestion 3\nWhat is your c?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question3.lower() == "a":
    print "Correct"
    count = count + 1
elif question3.lower() != "a":
    print "Wrong"

question4 = raw_input("\nQuestion 4\nWhat is your d?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question4.lower() == "a":
    print "Correct"
    count = count + 1
elif question4.lower() != "a":
    print "Wrong"

question5 = raw_input("\nQuestion 5\nWhat is your e?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question5.lower() == "a":
    print "Correct"
    count = count + 1
elif question5.lower() != "a":
    print "Wrong"

question6 = raw_input("\nQuestion 6\nWhat is your f?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question6.lower() == "a":
    print "Correct"
    count = count + 1
elif question6.lower() != "a":
    print "Wrong"

question7 = raw_input("\nQuestion 7\nWhat is your g?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question7.lower() == "a":
    print "Correct"
    count = count + 1
elif question7.lower() != "a":
    print "Wrong"

question8 = raw_input("\nQuestion 8\nWhat is your h?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question8.lower() == "a":
    print "Correct"
    count = count + 1
elif question8.lower() != "a":
    print "Wrong"

question9 = raw_input("\nQuestion 9\nWhat is your i?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question9.lower() == "a":
    print "Correct"
    count = count + 1
elif question9.lower() != "a":
    print "Wrong"

question10 = raw_input("\nQuestion 10\nWhat is your j?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question10.lower() == "a":
    print "Correct"
    count = count + 1
elif question10.lower() != "a":
    print "Wrong"

question11 = raw_input("\nQuestion 11\nWhat is your k?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question11.lower() == "a":
    print "Correct"
    count = count + 1
elif question11.lower() != "a":
    print "Wrong"

question12 = raw_input("\nQuestion 12\nWhat is your l?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question12.lower() == "a":
    print "Correct"
    count = count + 1
elif question12.lower() != "a":
    print "Wrong"

question13 = raw_input("\nQuestion 13\nWhat is your m?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question13.lower() == "a":
    print "Correct"
    count = count + 1
elif question13.lower() != "a":
    print "Wrong"

question14 = raw_input("\nQuestion 14\nWhat is your n?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question14.lower() == "a":
    print "Correct"
    count = count + 1
elif question14.lower() != "a":
    print "Wrong"

question15 = raw_input("\nQuestion 15\nWhat is your o?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question15.lower() == "a":
    print "Correct"
    count = count + 1
elif question15.lower() != "a":
    print "Wrong"

question16 = raw_input("\nQuestion 16\nWhat is your p?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question16.lower() == "a":
    print "Correct"
    count = count + 1
elif question16.lower() != "a":
    print "Wrong"

question17 = raw_input("\nQuestion 17\nWhat is your q?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question17.lower() == "a":
    print "Correct"
    count = count + 1
elif question17.lower() != "a":
    print "Wrong"

question18 = raw_input("\nQuestion 18\nWhat is your r?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question18.lower() == "a":
    print "Correct"
    count = count + 1
elif question18.lower() != "a":
    print "Wrong"

question19 = raw_input("\nQuestion 19\nWhat is your s?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question19.lower() == "a":
    print "Correct"
    count = count + 1
elif question19.lower() != "a":
    print "Wrong"

question20 = raw_input("\nQuestion 20\nWhat is your t?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D:")
if question20.lower() == "a":
    print "Correct"
    count = count + 1
elif question20.lower() != "a":
    print "Wrong"

print "\n|Congratulation!", name, ". You complete the test."
print "|Here's your result."
print "|Total score: " + str(count) + "/20"
division = float(count)/float(20)
multiply = float(division*100)
result = round(multiply)
print "|Total percentage is", int(result), "%"

if result >= 95:
    print ("|Grade: A+ \n|Well done!")

elif result >= 80:
    print ("|Grade: B \n|Good job!")

elif result >= 65:
print ("|Grade: C \n|You did okay.")

elif result >=50:
    print ("|Grade: D \n|Wow! That was close. You nearly fail.")

elif result >= 0:
    print ("|Grade: Fail\n|You should study more.")

{py} what i want is................

>>> 
Hello what is your name? bob
Hi bob
Are you ready to do the test?(Please enter YES/NO)no
Are you ready to do the test?(Please enter YES/NO)no
Okay, tell me when you're ready. 
Are you ready to do the test?(Please enter YES/NO)no
Okay, tell me when you're ready. 
Are you ready to do the test?(Please enter YES/NO)no
Okay, tell me when you're ready. 
Are you ready to do the test?(Please enter YES/NO)no
Okay, tell me when you're ready. 
Are you ready to do the test?(Please enter YES/NO)asdasda'
It okay if your're not sure. The test can start in any time you want. 
Are you ready to do the test?(Please enter YES/NO)hi
It okay if your're not sure. The test can start in any time you want. 
Are you ready to do the test?(Please enter YES/NO)yes
Excelent! The test will start in any second. 

Question 2
What is your b?

A
B
C
D

Please enter A, B, C or D:a
Correct


A
B
C
D

Please enter A, B, C or D:b
Wrong

Question 10
What is your j?

A
B
C    
D

Please enter A, B, C or D:b
Wrong

Question 11
What is your k?

A
B
C
D

Please enter A, B, C or D:b
Wrong

Q1.....

|Congratulation! bob . You complete the test.a

|Here's your result.

|Total score: 8/20

|Total percentage is 40 %

|Grade: Fail

|You should study more.
1

2 Answers 2

5

I suggest you use a dictionary to do this, the format being {question number: [answer, question], ...}: q_and_a = {1: ['a', 'What is k?'], 2: ['b', 'What is c?'], ...}.

Then you can create a list of the keys from this, and shuffle the list.

import random
q_and_a = {1: ['a', 'What is k?'], 2: ['b', 'What is c?'], ...}
questions = q_and_a.keys()
random.shuffle(questions)
for i in questions:
    my_msg = "\nQuestion " + str(i) + q_and_a[i][1] + "\nA\nB\C\nD"# gets the question
    answer = raw_input(my_msg)
    while answer != q_and_a[i][0]:
        print('Incorrect! Try again!')
        answer = raw_input(my_msg)

Some References:

Sign up to request clarification or add additional context in comments.

2 Comments

A dictionary is the way to go.
sorry but i am not sure about the question = q_a_.key() part。 and for i in question: part
1
from random import sample
d = {1: 'question A', 2: 'question B', 3: 'question C', 4: 'question D',
    5: 'question E', 6: 'question F', 7: 'question G'}
for result in [d.get(x, 'unknown') for x in sample(range(1,len(d)+1),len(d))]:
print result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.