My Python skills are incredibly basic and that's why you're about to look through a horrible mess.
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. 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.
This is an example of the output if I run the loop four times:
yak is the new frog
yak is the new frog
The yak is a frog
yak is the new frog
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.
Aucun commentaire:
Enregistrer un commentaire