Python中随机化多个选择

0 投票
2 回答
2254 浏览
提问于 2025-04-17 16:58

我想做的是把问题1到问题20的顺序随机打乱,而且在随机打乱后,我还可以在问题上输入我的答案。每次我尝试使用随机化的语句时,总是出现错误或者意外的错误。现在我的代码是按顺序打印问题。我要做的就是把问题用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.

2 个回答

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
5

我建议你用字典来实现这个功能,格式是这样的:{问题编号: [答案, 问题], ...}:

q_and_a = {1: ['a', 'k是什么?'], 2: ['b', '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)

一些参考资料:

撰写回答