gpt4 book ai didi

python - 随机填空

转载 作者:行者123 更新时间:2023-12-02 02:26:33 25 4
gpt4 key购买 nike

尽管这个填空脚本成功运行,但我不确定如何随机分配空白。可以看到,我在 5-7 之间放置了两个空格。但是,我想随机化它们的设置位置。

sentence = """Immigration is an issue that affects all residents of the United States, regardless of citizenship status"""
sentence0 = sentence.split(" ")
max = len(sentence)
sentence1 = sentence0[0:5]
sentence1 = " ".join(sentence1)
sentence2 = sentence0[7:max]
sentence2 = " ".join(sentence2)
Overall = sentence1 + " _ _ " + sentence2
print(Overall)
test = input()
Overall2 = sentence1 + " " + test + " " + sentence2
print(Overall2)
start = "\033[1m"
end = "\033[0;0m"
if Overall2 == sentence:
print(start + "Correct" + end)
else:
print(start + "Incorrect" + end)

最佳答案

这很简单并且有效:

import random

sentence = "Immigration is an issue that affects all residents of the United States, regardless of citizenship status"
words = sentence.split(" ")
#SELECT RANDOM WORD FOR PLACING BLANK
rand_index = random.randint(0, len(words)-1)
#KEEP A BACKUP OF THE WORD
word_blanked = words[rand_index]
#REPLACE WORD WITH BLANK
words[rand_index] = "_____"
#MAKE BLANKED SENTENCE AND CORRECT ANSWER
blanked_sentence = ""
correct_answer = ""
for word in words:
blanked_sentence = blanked_sentence + word + " "
if word == "_____":
correct_answer = correct_answer + word_blanked + " "
else:
correct_answer = correct_answer + word + " "

print(blanked_sentence)
answer = input("Enter your answer : ")
if answer == word_blanked:
print("Correct Answer!")
else:
print("Wrong Answer!")
print("Correct Answer is : ")
print(correct_answer)

关于python - 随机填空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65567240/

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