#python
#Python
Вопрос:
Несмотря на то, что этот скрипт заполнения пустого места работает успешно, я не уверен, как случайным образом назначать пробелы. Как видно, я поместил два пробела между 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 = "33[1m"
end = "33[0;0m"
if Overall2 == sentence:
print(start "Correct" end)
else:
print(start "Incorrect" end)
Комментарии:
1. Вы проверили
random
модуль?2. Я думаю, вы ищете random.randrange вместо 5 в вашем коде, чтобы добавить пробелы.
Ответ №1:
Это просто и работает :
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)
Ответ №2:
Что-то вроде этого:
import random
sentence = """Immigration is an issue that affects all residents of the United States, regardless of citizenship status"""
sentence0 = sentence.split(" ")
# removed overlap with "max"
max_length = len(sentence0)
# generate a random slice based on the length of the sentence.
random_slice = random.randrange(0, max_length)
sentence1 = sentence0[0:random_slice ]
sentence1 = " ".join(sentence1)
# increment two words forward from the random slice
sentence2 = sentence0[random_slice 2: max_length]
sentence2 = " ".join(sentence2)
Overall = sentence1 " _ _ " sentence2
print(Overall)
test = input()
Overall2 = sentence1 " " test " " sentence2
print(Overall2)
start = "33[1m"
end = "33[0;0m"
if Overall2 == sentence:
print(start "Correct" end)
else:
print(start "Incorrect" end)
Ответ №3:
Общий пример:
import random
sentence = 'The quick brown fox jumps over the lazy dog'
# convert sentence from string to list
sentenceList = sentence.split(' ')
# get random location of the element to be replaced
locToReplace = random.randrange(0, len(sentenceList))
# replace with blanks
sentenceList[locToReplace] = '_ _'
# convert back to string
updatedSentence = ' '.join(sentenceList)
print(updatedSentence)