-1

I'm using Python 2.7.13 and Windows Powershell. When I run the program, the first raw_input() isn't recognized, regardless of whether, "yes", "y", or "Yes", are entered.

Ideally, I'm wanting the program to register whether any of these options are initially used. If one of these options isn't used, the while loop continues to display the "Type 'yes' when you are ready to begin" message, until the appropriate input is received from the user. Any help would be greatly appreciated!

# This is a questionnaire form for a python programming exercise.

print "Hello, Sir or Madame.",
print "My name is Oswald, and I will be asking you a few questions, today."
print "Are you ready to begin?"
answer_1 = raw_input()

while answer_1 != "yes" or answer_1 != "y" or answer_1 != "Yes":
    print "Type 'yes' when you are ready to begin."
    answer_1 = raw_input()

    if (answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"):
        print "What is your name"
        name = raw_input()
        print "What is your favorite color?"
        color = raw_input()
        print "Where do you live?"
        home = raw_input()
        print "So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home)
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
  • 1
    Possible duplicate of [Why non-equality check of one variable against many values always returns true?](http://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Barmar Apr 14 '17 at 20:28
  • you can use `answer.lower()` to make sure that the input is in lower case regardless of what the user enter. – Astrom Apr 14 '17 at 20:31

4 Answers4

0

You have your condition backwards.

In Python (and programming in general), the or statement is true if ANY of the items mentioned is true.

So when you say answer != A or answer != B then, because answer can only have one value, the result will always be true. This is because, if answer is C, then it will be doubly-true, while if answer is A the "or answer !=B" part will be true, while if answer is B the "answer != A" part will be true.

If you are testing for a list of possible acceptable responses, you need to either make it an inclusive test using or:

if answer = A or answer = B or answer = C ...

or make it an exclusive test using and:

if answer != A and answer != B and answer != C ...

This is true for if statements and while statements.

aghast
  • 14,785
  • 3
  • 24
  • 56
  • Thank you for the wonderful lesson, however, as I commented above, changing my instances of or to and in the while loop portion actually causes the entire program to end. Am I not grasping something correctly? – Dakota Freeman Apr 14 '17 at 21:04
0

Alright, I believe I've gotten it to work how you want it to.

First things first: I made your condition evaluate to a single True or False by having it do this.

condition = ((answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"))

Then to start your while loop you just need to do this:

while not condition: 
print "Type 'yes' when you are ready to begin."
answer_1 = raw_input()
condition = ((answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"))

Then and this is the important part. You need to move the questions out side of the while loop. Because if the while evaluates to true, it will ask you repetitively if your ready. But say you answer yes at the beginning the while loop evaluates to False and the code for the questions is skipped. If you want it to ask the questions repetitively just put them in a

while True: 
#ask the questions

Oh also, you won't need the if statement if you do it this way. That is because if you answer no at the beginning, you'll enter the while loop until you enter yes. After which the loop closes, and the questions are asked.

I hope that answers your question. Keep up the good work!

Nalisarc
  • 113
  • 7
0

Try changing your while statement to be like this:

print "Are you ready to begin?"
answer_1 = raw_input()

while answer_1 == "yes" or answer_1 == "y" or answer_1 == "Yes":
    print "Type 'yes' when you are ready to begin."
    answer_1 = raw_input()
    if (answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"):
        print "What is your name"
        name = raw_input()
-1

You can actually simplify what you're trying to do:

answer_1 = ''
while answer_1.lower() not in ('y', 'yes', 'si', 'da', 'yes, please'):
    answer_1 = raw_input('Are you ready to begin? (type "yes" to begin): ')

And this will continue to ask for them to type... well, any of the possible inputs :)


Your problem is that you're getting the input twice:

print "Are you ready to begin?"
answer_1 = raw_input()  # <==== Once here

while answer_1 != "yes" or answer_1 != "y" or answer_1 != "Yes":
    print "Type 'yes' when you are ready to begin."
    answer_1 = raw_input()    # <==== again here

And then your if comes inside the while loop. But you don't need to do that.

It helps if you think about your program and write it out first without writing code:

  • while the user doesn't provide correct input, ask the user for input
  • then, ask them for their choices

What that means in the code is that you're going to have two separate blocks:

# Block 1
# while not_correct_input:
#     get input

# Block 2
# ask for choices

Now you can replace these with code that you need:

from __future__ import print_function

# Block 1
# while not correct input
#    get input

# Block 2
name = raw_input("What is your name? ")
color = raw_input("What is your favorite color? ")
home = raw_input("Where do you live? ")
print("So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home))

Once you try this out and it works, then you can go ahead and add your check for correct input:

# Block 1
answer_1 = ''
while answer_1.lower() not in ('y', 'yes', 'si', 'da', 'yes, please'):
    answer_1 = raw_input('Are you ready to begin? (type "yes" to begin): ')

# Block 2
name = raw_input("What is your name? ")
color = raw_input("What is your favorite color? ")
home = raw_input("Where do you live? ")
print("So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home))

Now when all of this works, go ahead and add the other parts of your program that you want:

from __future__ import print_function

print('''
Hello, Sir or Madame. My name is Oswald, and I will be asking you a few questions, today.
'''.strip())

# Block 1
answer_1 = ''
while answer_1.lower() not in ('y', 'yes', 'si', 'da', 'yes, please'):
    answer_1 = raw_input('Are you ready to begin? (type "yes" to begin): ')

# Block 2
name = raw_input("What is your name? ")
color = raw_input("What is your favorite color? ")
home = raw_input("Where do you live? ")
print("So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home))

Here's an example of what this code looks like in action:

Hello, Sir or Madame. My name is Oswald, and I will be asking you a few questions, today.
Are you ready to begin?
Are you ready to begin? (type "yes" to begin): no
Are you ready to begin? (type "yes" to begin): maybe
Are you ready to begin? (type "yes" to begin): probably
Are you ready to begin? (type "yes" to begin): okay
Are you ready to begin? (type "yes" to begin): yes
What is your name? Wayne
What is your favorite color? Blue... no yellow!
Where do you live? Camelot
So, you're 'Wayne'. You're favorite color is 'Blue... no yellow!', and you live in 'Camelot'
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • I tried this, but I'm still getting the skipped over first input. If I answer, "yes" or any other iteration of "yes" listed, it will still proc the "Type 'yes' when you are ready to begin." string. What am I doing wrong here? – Dakota Freeman Apr 14 '17 at 21:13
  • @DakotaFreeman Without seeing your code, I'm guessing that you left your original first `input` in there. I've added *exact* code samples, along with sample input/output. If my last example doesn't work then you've done something horribly wrong. – Wayne Werner Apr 17 '17 at 14:22