[英]Using a loop to make a random sentence generator
My Python skills are incredibly basic and that's why you're about to look through a horrible mess. 我的Python技能非常基础,这就是为什么您要经历一个可怕的混乱局面。
Basically, I want to use text files to make a list of words. 基本上,我想使用文本文件来制作单词列表。 Each file will have its own type of word (Nouns, verbs, etc.). 每个文件都有其自己的单词类型(名词,动词等)。 Then I want randomly pick words out of that list and fill them in the appropriate spots in a random sentence template, kind of like Mad Libs. 然后,我想从该列表中随机选择单词,然后将它们填充到随机句子模板的适当位置,就像Mad Libs一样。 My problem is when I try to fit everything into a loop. 我的问题是当我尝试将所有内容放入一个循环中时。 I get a random template each time, but I cannot figure out how to make the randomly selected word change every time. 我每次都会得到一个随机模板,但是我无法弄清楚如何使每次随机选择的单词都发生变化。
This is the function for reading the file and adding its contents to a list, which seems to work fine. 这是用于读取文件并将其内容添加到列表中的功能,似乎可以正常工作。
def get_Noun():
infile = open('Noun.txt', 'r')
nouns = infile.readlines()
infile.close()
index = 0
while index < len(nouns):
nouns[index] = nouns[index].rstrip('\n')
index += 1
Noun = random.choice(nouns)
return Noun
Next are functions for my two sentence templates and a function for randomly selecting one of them: 接下来是我的两个句子模板的功能和一个随机选择其中一个的功能:
Noun = get_Noun()
Noun2 = get_Noun()
def Cake():
get_Noun()
print("The", Noun, "is a", Noun2)
def XistheNew():
get_Noun()
print(Noun, "is the new", Noun2)
def get_Sentence():
num = random.randrange(1, 3)
if num == 1:
Cake()
elif num == 2:
XistheNew()
Lastly, I have a loop that controls the whole thing. 最后,我有一个控制整个过程的循环。 I thought about putting it into another "main" function, but that didn't seem to make a difference anyway. 我曾考虑过将其放入另一个“主”函数中,但这似乎并没有什么不同。
while sp == '':
get_Sentence()
sp = input('')
What I want to happen is for a new sentence with completely new words to pop up every time I hit enter. 我想发生的事情是,每当我按下Enter键时,就会弹出一个带有全新单词的新句子。
This is an example of the output if I run the loop four times: 如果我运行循环四次,这是输出的示例:
yak is the new frog y牛是新来的青蛙
yak is the new frog y牛是新来的青蛙
The yak is a frog 牛是青蛙
yak is the new frog y牛是新来的青蛙
The sentence that is chosen seems to be random, but the words do not change. 选择的句子似乎是随机的,但单词不会改变。 How can I fix this? 我怎样才能解决这个问题? I know my code is horribly chaotic and somewhat idiotic, but I plan to fix it up once I can get this one thing to work. 我知道我的代码非常混乱而且有些愚蠢,但是我计划一旦我能使这件事正常工作就对其进行修复。 I really appreciate any help. 我非常感谢您的帮助。 And like I said, I'm a complete noob so simple answers are appreciated if possible. 就像我说的,我是一个完整的菜鸟,因此,如果可能的话,我们欢迎您提供简单的答案。
You are only assigning noun
and noun2
once on program initialisation 你只分配noun
和noun2
在程序初始化一次
Try having a new function like so: 尝试具有如下新功能:
def set_nouns():
noun = get_noun()
noun2 = get_noun()
and then call set_nouns()
instead of get_noun()
in your Cake()
and XistheNew()
functions. 然后调用set_nouns()
代替get_noun()
在你的Cake()
和XistheNew()
函数。
Also, have a read through the python style guide . 另外,请通读python样式指南 。
Adding to Vikram's answer: 添加到Vikram的答案中:
your get_noun function relaods your whole file every time, which is a waste. 您的get_noun函数每次都会重新整理整个文件,这很浪费。 Consider having a function that loads your words (only once) to a list and then a function that chooses at random from that list. 考虑具有一个将单词(仅一次)加载到列表中的函数,然后再从该列表中随机选择一个函数。
Finally: replace all Noun and Noun2 by choose_noun(). 最后:用choose_noun()替换所有名词和名词2。
Just to elaborate on Vikram's (correct) answer, the issue is these lines: 为了详细说明Vikram的(正确)答案,问题在于以下几行:
Noun = get_Noun()
Noun2 = get_Noun()
They create two variables, Noun
and Noun2
, then run get_Noun
twice and assign the results to those variables. 他们创建两个变量Noun
和Noun2
,然后两次运行get_Noun
并将结果分配给这些变量。
Then, in the line: 然后,在该行中:
print("The", Noun, "is a", Noun2)
...You aren't rerunning get_Noun
. ...您不会重新运行get_Noun
。 Here, Noun
and Noun2
are references to the two strings created previously, not to the get_Noun
method. 在这里, Noun
和Noun2
是对先前创建的两个字符串的引用, 而不是对get_Noun
方法的get_Noun
。 At this point, the program doesn't care how you initially created those variables, it just re-uses their values. 在这一点上,该程序不在乎您最初如何创建这些变量,而只是重新使用它们的值。
To make the get_Noun
function run again, you have to explicitly call it again each time, eg as in Vikram's answer. 为了使get_Noun
函数再次运行,您必须每次都明确地再次调用它,例如在Vikram的答案中。 To illustrate the point, this would also work: 为了说明这一点,这也将起作用:
print("The", get_Noun(), "is a", get_Noun())
That line calls the function everywhere a new random value is needed. 该行在需要新随机值的地方调用该函数 。
In addition to all the answers provided I'd like to point out in your case you would change the code to 除了提供的所有答案外,我想指出的是您将代码更改为
def Cake():
print("The", get_noun(), "is a", get_noun())
def XistheNew():
print(get_noun(), "is the new", get_noun())
def get_Sentence():
num = random.randrange(1, 3)
if num == 1:
Cake()
elif num == 2:
XistheNew()
This way when Cake()
or XistheNew()
is called a new noun is created from your method get_noun()
这样,当Cake()
或XistheNew()
称为新名词时,将从您的方法get_noun()
创建
NOTE: I changed your noun varible to lowercase. 注意:我将您的名词变量更改为小写。 Like what Vikram linked, there is a convention to python coding style. 像Vikram所链接的一样,python编码风格也有约定。
Building on what Julien said you would add another method to load the file 在朱利安所说的基础上,您将添加另一种方法来加载文件
nouns = load_nouns()
def load_nouns():
infile = open('Noun.txt', 'r')
nouns = infile.readlines()
infile.close()
return nouns
def get_noun():
index = 0
while index < len(nouns):
nouns[index] = nouns[index].rstrip('\n')
index += 1
noun = random.choice(nouns)
return noun
This way your file isn't loading every time you call get_noun()
so your full file would look something like this 这样,每次调用get_noun()
时文件都不会加载,因此完整文件看起来像这样
nouns = load_nouns()
def load_nouns():
infile = open('Noun.txt', 'r')
nouns = infile.readlines()
infile.close()
return nouns
def get_noun():
index = 0
while index < len(nouns):
nouns[index] = nouns[index].rstrip('\n')
index += 1
noun = random.choice(nouns)
return noun
def Cake():
print("The", get_noun(), "is a", get_noun())
def XistheNew():
print(get_noun(), "is the new", get_noun())
def get_Sentence():
num = random.randrange(1, 3)
if num == 1:
Cake()
elif num == 2:
XistheNew()
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.