Meskipun skrip isi-kosong ini bekerja dengan sukses, saya tidak yakin bagaimana cara menetapkan yang kosong secara acak. Seperti yang bisa dilihat, saya menempatkan dua kosong antara 5-7. Namun, saya ingin mengacak di mana mereka diatur.

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)
2
Johnny 4 Januari 2021, 20:40

3 jawaban

Jawaban Terbaik

Ini sederhana dan berfungsi:

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
Jaysmito Mukherjee 4 Januari 2021, 18:36

Sesuatu seperti ini:


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 = "\033[1m"
end = "\033[0;0m"
if Overall2 == sentence:
    print(start + "Correct" + end)
else:
    print(start + "Incorrect" + end)

2
KJDII 4 Januari 2021, 17:58

Contoh umum:

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)
1
Ilya Berdichevsky 4 Januari 2021, 18:54