12 Replies - 1020 Views - Last Post: 25 October 2013 - 08:49 AM Rate Topic: -----

#1 jlanger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 13

Madlibs program

Posted 24 October 2013 - 07:38 AM

Hey guys, I was having trouble with this program and came to these forums hoping somebody could point me in the right direction. It's a rather simple idea but I cannot figure it out. The program is a madlibs program where the user inputs a filename and the program reads it in and then prompts the user for whatever is within square brackets within the input program.

For example,

input txt file:
After I got out of the shower and dressed, I [past-tense-verb] to the
student center to eat [noun] and [noun] for breakfast. I was [emotion]
to go to my classes as we were learning about [topic] . After dinner I
planned to [activity]. 


output:
Enter input filename: madlib1.txt
Enter output filename: story.txt
Enter a past-tense-verb: ran
Enter a noun: green eggs
Enter a noun: ham
Enter a emotion: excited
Enter a topic: loops
Enter a activity: go for a bike ride



after the program runs, it should then create an output file that looks like the following:
After I got out of the shower and dressed, I ran to the
student center to eat green eggs and ham for breakfast. I was excited
to go to my classes as we were learning about loops . After dinner I
planned to go for a bike ride .



So far, I have read the file in but I cannot solve how to code this. If somebody could help I would greatly appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Madlibs program

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8957
  • View blog
  • Posts: 33,585
  • Joined: 12-June 08

Re: Madlibs program

Posted 24 October 2013 - 08:03 AM

How are you reading in the text?

One would figure you can read in the file lines to a string-like data type.. then scan the string for bracketed items.. dump the found items in an array.. when done go through the array and ask for input.. store the input in a similarly sized array.. after the input is done you can go BACK through the original file-lines string and replace (in order of the array).
Was This Post Helpful? 0
  • +
  • -

#3 jlanger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 13

Re: Madlibs program

Posted 24 October 2013 - 09:47 AM

View Postmodi123_1, on 24 October 2013 - 08:03 AM, said:

How are you reading in the text?

One would figure you can read in the file lines to a string-like data type.. then scan the string for bracketed items.. dump the found items in an array.. when done go through the array and ask for input.. store the input in a similarly sized array.. after the input is done you can go BACK through the original file-lines string and replace (in order of the array).


This is how i'm reading it in
def main():

    fname = input('Enter input filename: ')
    infile = open(fname, 'r')
    infile = infile.read()



How would I go about scanning the string for bracketed items? I'm guessing you need to use the .split method and the .find method but i'm not sure how to use it to do what I need.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8957
  • View blog
  • Posts: 33,585
  • Joined: 12-June 08

Re: Madlibs program

Posted 24 October 2013 - 09:50 AM

Split.. sure.. or remember a string is an array and you can use a basic loop to stumble through each character and evaluate if they are a bracket.. if you find one then another string can go from that point until it finds the first end bracket..
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Madlibs program

Posted 24 October 2013 - 11:06 AM

Quick hint:
>>> s = 'The dog [activity] with the cat.'
>>> s.find('[',0)
8
>>> s.find('[',9)
-1
>>> 


Was This Post Helpful? 0
  • +
  • -

#6 jlanger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 13

Re: Madlibs program

Posted 24 October 2013 - 01:12 PM

I feel like I am doing this a very odd way but using this:
for i in infile:
        a = infile.find('[')
        for x in i:
            b = infile.find(']')



I got the correct spots for the brackets, but now how can I get the information in between..
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8957
  • View blog
  • Posts: 33,585
  • Joined: 12-June 08

Re: Madlibs program

Posted 24 October 2013 - 01:19 PM

Wow.. I wish there was a function to get a string from inside another string.. you know.. a substring!

http://docs.python.o...ary/string.html
Was This Post Helpful? 0
  • +
  • -

#8 jlanger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 13

Re: Madlibs program

Posted 24 October 2013 - 01:59 PM

Okay solved that problem but now I have an infinite loop. Why is it not moving on to the next set of brackets?
for i in infile:
        a = infile.find('[')
        for x in i:
            b = infile.find(']')
            word = infile.strip()[a+1:b]
            entry = input('Enter a '+word+': ')

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Madlibs program

Posted 24 October 2013 - 03:48 PM

Step 1: read then entire file into a string.

Step 2: work on the string.

What not to do: mess with a file while trying to read it as the same time.
Was This Post Helpful? 0
  • +
  • -

#10 jlanger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 13

Re: Madlibs program

Posted 24 October 2013 - 05:44 PM

View Postbaavgai, on 24 October 2013 - 03:48 PM, said:

Step 1: read then entire file into a string.

Step 2: work on the string.

What not to do: mess with a file while trying to read it as the same time.


I'm confused... didn't I do that already with this:
infile = open(fname, 'r')
    infile = infile.read()

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,742
  • Joined: 19-March 11

Re: Madlibs program

Posted 24 October 2013 - 06:35 PM

Python is a lot easier to use if you think of expressions rather than iterations wherever possible.

Try this and see if you can make use of the result:
[i.split('[') for i in infile.split(']')]


Note: you can't do input in a listcomp, so you'll have to iterate over the expression produced by this, but you'll be able to produce a list of expressions that you can glue together with a simple "".join() when you're done.

This post has been edited by jon.kiparsky: 24 October 2013 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Madlibs program

Posted 25 October 2013 - 02:07 AM

View Postjlanger, on 24 October 2013 - 08:44 PM, said:

I'm confused... didn't I do that already with this:
infile = open(fname, 'r')
    infile = infile.read()


Whoa... Ok, my bad. I hadn't seen you'd overwritten your file handle with the result from the read you used it for. So, infile is a string now. Carry on.

No, wait, why the hell would you do that? You didn't close your file, you now can't, and your variable name is bizarre.

Please, don't do that. At least close the file. e.g.
with open(fname, 'r') as fh: # creates a block that will close the file
    data = fh.read() # use a variable name that make sense

print data # you got the data and the file is now closed


Was This Post Helpful? 0
  • +
  • -

#13 jlanger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 13

Re: Madlibs program

Posted 25 October 2013 - 08:49 AM

Okay finally solved it. Thanks for the help guys
{code}def main():

fname = input('Enter input filename: ')
oname = input ('Enter output filename: ')


infile = open(fname, 'r')
outfile = open(oname, 'w')


for line in infile:
for word in line.split():

if word[0] == '[':
madlib = word[1:(len(word)-1)]
userMadlib = input("Enter a "+madlib+": ")
print(userMadlib, file=outfile, end = ' ')
else:
madlib = word
print(madlib, file=outfile, end = ' ')


infile.close()
outfile.close()

main()
{/code]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1