0

私がやろうとしているのは、質問1から質問20を異なる順序でランダム化することです。ランダム化すると、質問に回答を入力することもできます。randomise ステートメントを使用しようとするたびに、常にエラーが発生するか、エラーが発生します。コードが今行うことは、質問を順番に出力することです。私がしなければならないことは、if ステートメントと elif ステートメントを一緒に使用して質問をランダム化することだけです

{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.
4

2 に答える 2

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)

いくつかの参考文献:

于 2013-02-24T22:52:58.757 に答える