points = 0
with open("ComputerScience.txt","r") as f:
for line in f:
y = line.split(",")
question = y[0]
question1 = question
del y[0]
random.shuffle(y)
answer1 = y[0]
answer2 = y[1]
answer3 = y[2]
answer4 = y[3]
print(question1+"\n","(A)",answer1,"\n","(B)",answer2,"\n","(C)",answer3,"\n","(D)",answer4)
correctans = answer1
userans = input("Enter A,B,C,D: ")
while userans == correctans:
points = (points+1)
print(points)
“ >>
我如何随机化选项,并让用户输入A B C或D并为每个正确答案添加分数
points = 0,其中open(“ ComputerScience.txt”,“ r”)为f:对于f中的行:y = line.split(“,”)问题= y [0]问题1 =问题del y [0] random.shuffle(y)...
夫妇:
LETTERS = ['A', 'B', 'C', 'D']
score = 0
with open("ComputerScience.txt","r") as f:
for line in f:
question, *answers = map(str.strip, s.split(','))
answers_jumbled = random.sample(answers, len(answers))
print(question)
for index, answer in enumerate(answers_jumbled):
print(f' ({LETTERS[index]}) {answer}')
answer = None
while answer not in LETTERS:
answer = input("Enter A,B,C,D: ")
if LETTERS.index(answer) == 0:
print('Correct')
score += 1
else:
print('Wrong')
print('Score:', score)
您需要在对数组进行混排之前使用correctans = answer1
行。
import random