Jump to content

Madlib Python failure

- - - - -

  • Please log in to reply
3 replies to this topic

#1
prgrmr

prgrmr

    Newbie

  • Members
  • Pip
  • 2 posts
I just started learning python today using python 2.6.6.
I decided to create a madlib program just for fun and here is what I coded.

print 'This is a madlib.'


#obtaining variable data

a = raw_input('Please type a noun:')

b = raw_input('Please type a second noun:')

c = raw_input('Please type a past tense verb:')

d = raw_input('Please type a his/her/herm:')

e = raw_input('Please type a title:')


print 'the' a' and the 'b

'- Hans Christian Anderson'


'ONCE upon a time there was a prince who wanted to marry a 'a'; but she would have to be a real'a'. He 'c 'all over the world to find one, but nowhere could he get what he wanted. There were 'a'es enough, but it was difficult to find out whether they were real ones. There was always something about them that was not as it should be. So he came home again and was sad, for he would have liked very much to have a real'a'.'


'One evening a terrible storm came on; there was thunder and lightning, and the rain poured down in torrents. Suddenly a knocking was heard at the city gate, and the old king went to open it.'


'It was a 'a 'standing out there in front of the gate. But, good gracious! what a sight the rain and the wind had made 'd 'look. The water ran down from 'd 'hair and clothes; it ran down into the toes of 'd 'shoes and out again at the heels. And yet she said that she was a real 'a'.'


'"Well, we will soon find that out," thought the old queen. But she said nothing, went into the bed-room, took all the bedding off the bedstead, and laid a 'b 'on the bottom; then she took twenty mattresses and laid them on the 'b', and then twenty eider-down beds on top of the mattresses.'


'On this the 'a 'had to lie all night. In the morning she was asked how she had slept.'


'"Oh, very badly!" said she. "I have scarcely closed my eyes all night. Heaven only knows what was in the bed, but I was lying on something hard, so that I am black and blue all over my body. It is horrible!"'


'Now they knew that she was a real 'a 'because she had felt the 'b 'right through the twenty mattresses and the twenty eider-down beds.


'Nobody but a real 'a 'could be as sensitive as that.'


'So the prince took 'd 'for his 'e', for now he knew that he had a real 'a'; and the 'b 'was put in the museum, where it may still be seen, if no one has stolen it.'


'There, that is a true story.' 

When i ran it, here is what i got:
  File "test.py", line 10

    print 'the' a' and the 'b

                  ^

SyntaxError: invalid syntax


it didn't even ask me my input. it works when i remove the story.

Can anyone help me?

Thanks in advance.

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
You need to put commas or pluses between variable names and string literals.

a = "string"

b = "another string"

print "value of a is: ", a, " value of b is: ", b

You can also do it like this:

a = "text"

b = "random"

print "This %s is %s." % (a, b), "Or this %s is %s." % (b, a)


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
prgrmr

prgrmr

    Newbie

  • Members
  • Pip
  • 2 posts
I used the first method and it worked. i couldn't understand the second method.

but thanks... all is good now

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
Second is C printf style, if you've heard of it. Basically every %s is replaced by a variable's value (s in this case stands for string, you can use i or d for integers and f for floating point numbers).

name = str(raw_input("Input your name: "))

age = int(raw_input("Input your age: "))


print "Hello %s, you're %d years old." % (name, age)

% has similar meaning like comma that you've used.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users