0

I'm starting out at python. I keep getting an error when I put a new raw_input inside an if statement. If the input they answered is incorrect I want to give them another chance to enter more input, is there another way to do this?

Heres the Code

attraction = {
    'approach' : "words",
    'introduction' : "words",
    'qualification' : "words",
    'rapport' : "words"
}

answer = raw_input("Pick from these approaches: introduction, qualification, or rapport:")

if answer != "approach" and answer != "introduction" and answer != "qualification" and answer != "rapport":
    new_answer = raw_input("You didn't choose one of the choices, type in the choice you want again:")
if new_answer != "approach" and answer != "introduction" and answer != "qualification" and answer != "rapport":
    print "That was your last chance buddy"
else:
    print attraction[answer]
9
  • put the raw input in a while loop. Use break when you have an answer Commented Dec 7, 2014 at 4:43
  • I know this is not a related question, but, may I ask why did you start out learning Python 2.7? Why not Python 3? Commented Dec 7, 2014 at 4:48
  • 1
    Not related to your question: it's easier to write: if answer not in ("approach", "introduction", "qualification", "rapport"): Commented Dec 7, 2014 at 4:48
  • I think you should use or expression instead of `and' because single input can't be of 3 different type. Commented Dec 7, 2014 at 4:49
  • You can also do if answer not in attraction.keys(): Commented Dec 7, 2014 at 4:49

2 Answers 2

2

You don't need to make a new variable new_answer when you're asking them the second time, you can just set answer to a new value:

answer = raw_input("You didn't choose one of the choices, type in the choice you want again:")

And replace new_answer with answer elsewhere in your program.

I would do the program like this, using the .keys() method:

attraction = {
    'approach' : "words",
    'introduction' : "words",
    'qualification' : "words",
    'rapport' : "words"
}

answer = raw_input("Pick from these approaches: introduction, qualification, or rapport:")

while not answer in attraction.keys():
    answer = raw_input("You didn't choose one of the choices, type in the choice you want again:")

print attraction[answer]
Sign up to request clarification or add additional context in comments.

3 Comments

you don't even need to use .keys(). By definition a dictionary iterates over it's keys - so you can simply use while not answer in attraction:
And that's what I get for not actually running the code in my answer.
@PeterWood Note that the in-loop raw_inputs have a different prompt than the initial one.
0

You can simply write:

while True:
    answer = raw_input("Pick from these approaches: introduction, qualification, or rapport:")
    if answer == "approach" or answer == "introduction" or answer == "qualification" or answer == "rapport":
        break

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.