Hello. For some reason line 3 - text = " I" - is being skipped. Thanks in advance!
def plotsentence1():
rand = random.randint(1,2)
text = "I "
x = random.randint(0,7)
text = Plotverb[x]
text = text + " the "
if rand == 1:
text = text + Setting[place - 1]
if rand == 2:
x = [themevector1, themevector2]
theme = random.choice(x)
adjective_choices = random.sample(Adjective, 12 + theme)
text = text + random.choice(adjective_choices)
text = text + " "
text = text + Setting[place - 1]
text = text + "."
return text
Why do you think line three is being skipped? Is it because your result doesn’t start with "I "? If so, note that line five (text = Plotverb[x]) overwrites the value currently referred to by text. Once that line executes the result of line three is discarded.
In addition, repeatedly appending more strings may be inefficient. Instead, collect the components in a list, then build the final string in one operation.
def plotsentence1():
pieces = ["I"]
rand = random.randint(1,2)
x = random.randint(0,7)
pieces.append(Plotverb[x])
pieces.append("the")
if rand == 1:
pieces.append(Setting[place - 1])
elif rand == 2:
x = [themevector1, themevector2]
theme = random.choice(x)
adjective_choices = random.sample(Adjective, 12 + theme)
pieces.extend([random.choice(adjective_choices), Setting[place - 1])
text = ' '.join(pieces) + "."
return text
That doesn’t look the same as my code. Here’s mine:
""" Generate plot sentence one """
def plotsentence1():
rand = random.randint(1,2)
text = "I "
x = random.randint(0,7)
text = text + Plotverb[x]
text = text + " the "
if rand == 1:
text = text + Setting[place - 1]
if rand == 2:
x = [themevector1, themevector2]
theme = random.choice(x)
adjective_choices = random.sample(Adjective, 12 + theme)
text = text + random.choice(adjective_choices)
text = text + " "
text = text + Setting[place - 1]
text = text + "."
return text