2013-02-24 112 views
0

我試圖做的是以不同的順序隨機化question1到question20,當它是隨機的,我也可以在問題中鍵入我的答案。 每次我嘗試使用randomise語句時,總會出現錯誤或意圖錯誤。 現在的代碼是按順序打印問題。 所有我需要做的就是隨機化與if和elif的語句問題一併Python在Python中隨機選擇多個選擇

{} 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 我想是........... .....

>>> 
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. 
+0

[洗牌在python對象的列表(可能重複http://stackoverflow.com/問題/ 976882 /洗牌對象在Python中的列表) – Celada 2013-02-24 22:51:44

+5

可怕的代碼btw – mou 2013-02-24 23:02:27

回答

5

我建議你用字典來做到這一點,格式與{問題數答案問題],...}: q_and_a = {1: ['a', 'What is k?'], 2: ['b', 'What is c?'], ...}

然後您可以從中創建一個密鑰列表,並隨機播放列表。

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) 

一些參考文獻:

+0

字典是要走的路。 – 2013-02-24 23:02:14

+0

對不起,但我不確定question = q_a_.key()部分。對於我疑問:部分 – markh 2013-02-25 04:42:49

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