0

So I coded a function that takes the value of a batteries variable. I want it to do something if the value is "yes" or "no". If the value is none of these answers, I want it to to ask it again for an indefinite amount of times. Here is my code in case my description was bad.

def batterie_answer():
    batteries = raw_input("yes or no > ").lower()
    print batteries
    while True:
        if batteries != "yes" or batteries != "no":
            print "Please respond yes or no"
            raw_input("> ")
            continue
        elif batteries == "yes":
            print "batteries taken!"
            items["batteries"] = 1
            break
        elif batteries == "no":
            print "Probably a wise choice. Save some space!"
            break

batterie_answer()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    See https://stackoverflow.com/a/23294659/1540468 – Paul Rooney Aug 15 '17 at 00:54
  • Your second use of `raw_input` should be identical to your first use: use lower and assign to batteries (aside: `answer` is probably a better variable name than `batteries`). –  Aug 15 '17 at 00:57

3 Answers3

1

You need to move the assignment to inside the while loop or add another assignment. You also need to change the or to an and. You would also need to remove the extra raw_input line that does not assign the value to the batteries variable.

def batterie_answer():
    while True:
        batteries = raw_input("yes or no > ").lower()
        print batteries
        if batteries != "yes" and batteries != "no":
            print "Please respond yes or no"
            continue
        elif batteries == "yes":
            print "batteries taken!"
            items["batteries"] = 1
            break
        elif batteries == "no":
            print "Probably a wise choice. Save some space!"
            break

batterie_answer()
tdube
  • 2,453
  • 2
  • 16
  • 25
  • Hey I'm a noobie in coding, so that's why I ask easy questions, but can you explain to me why I have to put batteries assignement line in the while loop and why I have to put an and instead of an or. –  Aug 15 '17 at 01:12
  • If you don't put the assignment line inside the while loop, then the value of the variable never changes while you're in the loop, which means you can never get out of it. – kindall Aug 15 '17 at 01:24
  • And the reason you have to use `and` rather than `or` is that with `or`, that expression is always true. If `batteries` is equal to `yes` then it is not equal to `no` so the second part is true, and vice versa. Therefore at least one branch of the `or` is always true and that makes the whole thing true. – kindall Aug 15 '17 at 01:26
0

Your while loop doesn't include the original raw_input() command, so it's never going to ask for input after the first iteration. Instead, it will only ever print out the answer over and over again.

Move the while True up a few lines so that it encompasses the raw_input() command.

Soviut
  • 88,194
  • 49
  • 192
  • 260
  • The while loop does have a `raw_input` line. Fixing that line would probably be more instructive to the OP than just ignoring it. – zondo Aug 15 '17 at 00:58
0

The problem is in your if statement. It doesn't matter what value is given, the condition will always equate to true and it will ask again for a value. For example, if the value was "yes" then the condition would be (false or true) which equates to true, if the value was "no" then it would be (true or false) which also equates to true, if the value was something like "asdf" then it would be (true or true) which also equates to true.

to fix this change the "or" operator to an "and" operator.

if batteries != "yes" and batteries != "no":
Chris Thornton
  • 162
  • 2
  • 12