I'm new to python and I'm trying to pick random words from sevaral list stored in a dictionary. I'm writing a storybased MadLib without user input. I want some of the words to give me different list results based on the random choice that comes up. But I'm having trouble writing the code.
I have tried writing an IF-statement in the GET_WORD function, but with no good results. Any help will be usefull. Thanx :)
Tried this:
if local_dict['opt1'].pop("case B):
case B()
And tried this:
def create_story():
local_dict = copy.deepcopy(word_dict)
return story.format(
get_word('opt1',local_dict),
if get_word('opt1',local_dict)=get_word('opt1',local_dict(2)):
return word_dict_opt1B(),
get_word('opt2',local_dict),
get_word('alt1',local_dict)
)
def word_dict_opt1B = {
'opt3': ['case I','case J','case K']
}
THE CODE
import random
import copy
# Inspirert av Mad Libs Random Story Generator
story = (
"ACT I.\n " +
"As you venture into the world. {} calls your attention. Here you encounter {}.\n" +
"Here is your {}"
)
# create a dictionay
word_dict = {
'opt1': ['case A','case B','case C'],
'opt2':['case D','case F','case H'],
'alt1':['alt A.1','alt A.2'],
}
def get_word(type, local_dict):
words = local_dict[type];
cnt = len(words)-1
index = random.randint(0, cnt)
return local_dict[type].pop(index)
def create_story():
local_dict = copy.deepcopy(word_dict)
return story.format(
get_word('opt1',local_dict),
get_word('opt2',local_dict),
get_word('alt1',local_dict)
)
print ("Story 1: ")
print (create_story())
print ("")
print ("Story 2: ")
print (create_story())
Aucun commentaire:
Enregistrer un commentaire