I want to create a script which should prevent equal sentences. Firstly, I thought that it was enough to just check if the previous and the current sentence are the same. But it turned out that no sentence should be duplicate. My first thought was to create a new list and save the created sentences in it. After that, the program should check if the new sentence is on the list. But it doesn't work at all and I don't know why. I hope you can help me.
*If you find any syntax mistakes it's because I translated the script to English and did a mistake.
import random
sentence = ''
previous_sentence = ''
sentence_list = []
def create_sentence():
names = ["x","y", "z"]
descriptions = ["a","b", "Dc"]
global sentence
global previous_sentence
global sentence_list
while sentence == previous_sentence:
sentence_list.append(sentence)
name = random.choice(names)
description = random.choice(descriptions)
sentence = f'{name} is a {description}'
if sentence == previous_sentence and sentence in sentence_list:
name = random.choice(names)
description = random.choice(descriptions)
sentence = f'{name} is a {description}'
else:
previous_sentence = sentence
return sentence
else:
prevoius_sentence = sentence
return sentence
for i in range(50):
print(create_sentence())
CodePudding user response:
The script you've provided looks like it should prevent duplicate sentences from being generated, but there are a couple of issues with it that might be causing it not to work as intended.
First, in the while loop, you are adding the sentence to the sentence_list before checking if it's a duplicate. So even if it's a duplicate, it will still be added to the list, which means that the check for duplicates later on will not work correctly.
Next, inside the while loop, you have an if-else block where you are re-assigning the sentence variable with a new sentence if the current sentence is a duplicate. However, this block should be inside the while loop so that it's executed every time a duplicate sentence is generated.
Additionally, you are using the same variable names for checking duplicate sentences and for the list of sentences, this can lead to confusion.
Here's a revised version of your script that should work correctly:
import random
previous_sentence = ''
sentence_list = set()
def create_sentence():
names = ["x","y", "z"]
descriptions = ["a","b", "Dc"]
global previous_sentence
global sentence_list
sentence = f'{random.choice(names)} is a {random.choice(descriptions)}'
while sentence in sentence_list:
sentence = f'{random.choice(names)} is a {random.choice(descriptions)}'
sentence_list.add(sentence)
previous_sentence = sentence
return sentence
for i in range(50):
print(create_sentence())
CodePudding user response:
I rewrite your code in a bit cleaner way without global variables and to make this code more reusable:
import random
def create_sentence(names, descriptions, sentence_list):
name = random.choice(names)
description = random.choice(descriptions)
sentence = f'{name} is a {description}'
while sentence in sentence_list:
name = random.choice(names)
description = random.choice(descriptions)
sentence = f'{name} is a {description}'
sentence_list.append(sentence)
return sentence
def main():
names = ["x","y", "z"]
descriptions = ["a","b", "Dc"]
sentence_list = []
for i in range(50):
print(create_sentence(names, descriptions, sentence_list))
if __name__ == '__main__':
main()
- The
create_sentence()
function uses awhile
loop that continues generating new sentences until a sentence is generated that is not already in thesentence_list
. Once a unique sentence is generated, it is added to thesentence_list
using thesentence_list.append(sentence)
line. - The function also makes sure that a new sentence is created until it is different from the previous sentence, by checking if the sentence is already in the list before it appends it to the list. This ensures that the list will only contain unique sentences.
- The function
create_sentence()
is separated from the main logic of the program, making it more reusable and easier to test. - The function takes in the
names
,descriptions
, andsentence_list
as arguments, making it more clear what data the function depends on. - The function uses a
while
loop instead of global variables to keep track of previous sentence. - The
main
function creates the variables and calls thecreate_sentence
function in a for loop. - The
main
function is wrapped in aif name == 'main'
block, it allows the code to be imported as a module without running themain
function. - The variable and function names are in lower_case_with_underscores, making it more readable and consistent.
- The code is more readable and easy to understand.
- The
if else
block is removed as it's not required.