Pop Quiz


Hi there. There is no real issue with this excersice, but I wanted to try something for fun. In the code, the program asks what your favourite food is, you can answer anything except dogfood and it will respond to it. I want it to give a different message when I put in: dogfood

It just gives a syntax error. Could anyone elaborate on this?

print 'Welcome to the Pig Latin Translator!'

# Start coding here!
original = raw_input("What is your favourite food?")
if len(original) > 0 and original.isalpha():
    print "I also like",
    print original
elif original = dogfood:
    print "I don't like"
    print 'You can only fill in letters! Try again.'

This is purely for me understanding how to do it, because I'm curious on what I could do with this. Thank you


dogfood should be a string, currently it is a (undefined) variable.

Also, a single equal sign means assign, what do we use when we want to check if two things are equal?


So i should make a line above it saying: dogfood = "dogfood" and then use dogfood ?
And I guess I should have used == since that means they are equal.


yea, or just turn the variable into a string:

elif original == "dogfood":

i don't see why you would need to create a separate variable for this

And yes, you should use == to check if things are equal


Thanks but, it now says: I also like dogfood. So it just straight goes to the if and not the elif. Can I exempt the word dogfood from if and make it go to elif instead?


then i would do the following:

# check if user enters something valid
if len(original) > 0 and original.isalpha():
    # check if user enters dogfood
    if original == "dogfood":

add two else: one to print if the user not enters dogfood, and one for invalid user input. Make sure to get the indent right


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.