0

Okay, so the answer is probably obvious, but I don't know the correct way to get the program to respond differently depending on what the user types.

octopusList = {"first": ["red", "white"],
            "second": ["green", "blue", "red"],
            "third": ["green", "blue", "red"]}
squidList = ["first", "second", "third"]

squid = random.choice(squidList)
octopus = random.choice(octopusList[squid])
The lists and the squid and octopus generate a random phrase
resp = raw_input("Please Type Something")
while resp !=1:
    if resp == octopusList:
        print squid + " " +octopus
        break
    elif resp == "Something":
        print "Elephants are pachyderms"
        break
    else:
        print "That's another text to think about."
        break

print "One More Comment"
This is supposed to print squid + " " +octopus if the user inputs anything on the octopusList. Or, if the user types "Something", it should return the phrase "Elephants are pachyderms". If the user types anything esle, it should return the phrase "That's another text to think about." Lastly it should print "One More Comment". What it actually does is skip strait to else no matter what the user types.

It goes strait to Else so there's something I'm not getting about the if and the elif... thanks for any light you can shine on this.

2
  • Please edit your question and format all your code. It's difficult to figure out what this is supposed to do, especially since it's Python. Commented Jul 16, 2010 at 19:23
  • Even after indenting it, I'm still not quite sure what the program is supposed to do... Commented Jul 16, 2010 at 19:24

3 Answers 3

3

Edit: After Ned Batchelder formatted the code, I re-read the question and see my guess as to what you want to do wasn't quite correct ... although it's still not 100% clear if you're trying to loop or not. If you only want to go through this once, there's no need for the while loop, just remove it. As for the test of the input against the values in the dictionary called octopusList, you could use an in test, but that only tests against the dict's keys. If you're looking for the values in the dict entries, you'll have to either make octopusList a true list (e.g. octopusList = ['red', 'white', 'blue', 'green'] and do an in test (e.g. if resp in octopusList:) or get a little more complicated in parsing the dict.

Original answer: It seems you're trying to create a loop to get user input and print a string in response to that input. If that's the case, I see a few things in need of correction:

  • You do not want the break statements, as they cause the loop to exit.
  • As others have said, comparing the input to octopusList won't work, just compare it to your randomly generated octopus value.
  • If you're going to use 1 as an exit input, you need to test the input against the string form ('1'), not the integer form (1).
  • If my guess is correct about what you're doing, the raw_input needs to be inside the loop.

This isn't marked homework, so I've posted code to do what I think you want below. If you want the octopus value to remain unknown to the user, just delete the print octopus line, but I put it in so you know what value to enter for testing.

import random
octopusList = {"first": ["red", "white"],
            "second": ["green", "blue", "red"],
            "third": ["green", "blue", "red"]}
squidList = ["first", "second", "third"]

squid = random.choice(squidList)
octopus = random.choice(octopusList[squid])
print octopus # So we know what value to use - necessary for testing at least
resp = ''
while resp != '1':
    resp = raw_input("Please Type Something: ")
    if resp == octopus:
        print squid + " " +octopus
    elif resp == "Something":
        print "Elephants are pachyderms"
    else:
        print "That's another text to think about."
    print "One More Comment"

If my speculations & suggestions are off the mark please provide a better explanation as to what you're attempting to do.

Sign up to request clarification or add additional context in comments.

Comments

1

Comparing a string to a dictionary does not do anything useful (it will always be false, IIRC) -- did you maybe want 'if resp in octopusList'? I'm not sure why the second conditional never hits, perhaps you typed "something" instead of "Something"?

Comments

0

You're comparing a string (resp) with a list. Did you want something like this:

if resp == octopus:  # not octopusList!
    # do something
else:
    # ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.