gpt4 book ai didi

python - 创建多项选择测验python

转载 作者:太空宇宙 更新时间:2023-11-04 04:51:46 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来创建一个包含多项选择测验的文件,供人们打印或做任何他们想做的事情。我知道如何编写文件,但我仍然停留在如何为问题做出选择上。这个问题会问国家的首都,选择会给出 4 种可能性,只有一种是正确的。这是代码:

import random

state_capitals = A DICTIONARY of the states and their capitals which I spared you because it was really big :D


file = open(r'''C:\Users\Leo\Desktop\Quiz1CSL.txt''',"w")
#The range(1) is just there as a place holder for later
for x in range(1):

file.write("Austin's Computer Science State Capitals Quiz\n")

for x in range(10):
random_state = random.choice(list(state_capitals.keys()))
main_typed = "What is the capital of"
question_mark = "?"
file.write("\n")
file.write('{0} {1}{2}\n'.format(main_typed, random_state,question_mark))

file.close()

最佳答案

我删除了你的文件写入代码,看看标记的行如何从你的字典中获得 3 + 一个正确答案的样本:

import random

# testdata "Capitals" as states, "lowercase" as capital ;)
state_capitals = dict([(chr(b).upper(),chr(b)) for b in range(ord("a"),ord("z")+1)])

for x in range(10):
random_state = random.choice(list(state_capitals.keys()))
main_typed = "What is the capital of"
question_mark = "?"

choices = sorted( random.sample([state_capitals[x] for x in state_capitals if x != random_state],3) + [state_capitals[random_state]])
# random.samples(list,n) gives you n unique random values from list
# and I add in the "correct" one, by sorting them you have a uniform
# a to z order that lets the correct one vanish into the others.

print('{0} {1}{2}'.format(main_typed, random_state,question_mark))
print('Choices: ' + ",".join(choices))

Michael Butchers很好的建议归结为:

for x in range(10):
choices = random.sample(list(state_capitals),4)
random_state = random.choice(choices)
main_typed = "What is the capital of"
question_mark = "?"

print('{0} {1}{2}'.format(main_typed, random_state,question_mark))
print('Choices: ' + ",".join([state_capitals[x] for x in choices]))

输出:

What is the capital of F?
Choices: f,i,p,r
What is the capital of P?
Choices: h,j,p,w
What is the capital of J?
Choices: g,i,j,v
What is the capital of B?
Choices: b,n,s,w
What is the capital of C?
Choices: c,p,s,z
What is the capital of U?
Choices: g,l,u,w
What is the capital of C?
Choices: c,g,h,t
What is the capital of B?
Choices: b,o,y,z
What is the capital of R?
Choices: e,k,r,w
What is the capital of P?
Choices: b,p,x,z

关于python - 创建多项选择测验python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48132233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com