Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

for python, I need to make a mad libs game where in i would should be loading this template: For my [ a family member ] ’s [ an event ] present about [ a number ] years ago , 2 I got him a [ a noun ] then I am going to have to ask for user prompts to fill in the [] to complete the sentences. Please help?

OpenStudy (farmdawgnation):

Where are you stuck? It would be better if you could start and then ask specific questions about where you're getting hung up. You're more likely to get help that way. :)

OpenStudy (anonymous):

I am able to load the txt file in python.. my problem is I don't know how to start. I am assuming that I somehow have to gather the words enclosed in a bracket and find a way to substitute those words into the user input that i will have to ask. The goal of this task is to use dictionary, but I have no idea how to get it started. I tried splitting the words, but I seem to not get the format I want..

OpenStudy (farmdawgnation):

So, you have a few options. The best option is using regular expressions. I'm not fluent enough in python to tell you how complex that would be. But essentially, you can use regular expressions to parse out that stuff in between the brackets. If you're able to use a different character for your substitutions, you could use a vertical bar like this: | Then if you do a string split on the vertical bar every even numbered index in the array would be part of the sentence and every odd numbered index in the array would be a prompt to give to the user. Then just use these indexes in a for loop to build the resulting sentence. In pseudo code that woud look something like: splitArray = stringFromText.split("|") finalSentence = new String for(i = 0; i < splitArray.length; i ++ ) if i is even then finalSentence += splitArray[i] else print(splitArray[i]) userInput = promptUserForInput() finalSentence += userInput end end Those are my first two ideas, at least.

OpenStudy (anonymous):

How'd you make the vertical bar? sorry I'm new to programming. And wouldn't be splitting it by means of the vertical bar the same as splitting using the bracket?

OpenStudy (farmdawgnation):

The vertical bar should be on the same key as \ on your keyboard. It is on mine, at least. The brackets are different because you actually have two characters you need to split on: "[" and "]". That's harder to do with a simple string split.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!