-2

I have a problem with my code that I can't find the solution as well. I ask for questions that has to be valid but the loops just continues, and let me input.

print('Do you want to go to the store or woods?')

lists = ('woods', 'store')
while True:
    answers = input()    
    if answers == 'store':
        break
        print('Going to the store...')
    elif answers == 'woods':
        break
        print('Going to the woods...')
    while lists not in answers:
        print('That is not a valid answer')
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
C9 Fox
  • 17
  • 5
  • 2
    Do you understand that `while lists not in answers:...` loops forever and that you never modify `answer` in that subloop? That means you will never get to the top of the `while True:...` loop. – callyalater Aug 17 '16 at 16:02
  • 1
    Hi C9 Fox - can you tell us what investigations you have already tried, and what you have found out? For example, have you tried stepping through your code with a debugger? A good Stack Overflow question must demonstrate that you have already done "Search and Research". See the help section's [How To Ask](http://meta.stackexchange.com/help/how-to-ask) guide for more background. – Vince Bowdren Aug 17 '16 at 17:02

3 Answers3

2

You want to check if the user's answer is not in your list of valid answers. What you're doing is the other way around. Try this:

if answers not in lists:
    print('That is not a valid answer')

You'll also want to either break at that point, or print your prompt message again.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1

Try this:

print('Do you want to go to the store or woods?')
places = ('woods', 'store')
while True:
    answer = input()
    if answer in places:
        print ("Going to the {0}...".format(answer))
        break
    else:
        print('That is not a valid answer')
Fuk Yu
  • 173
  • 1
  • 9
1

First of all, your print statements are unreachable. You can find more information here.

#...
if answers == 'store':
        print('Going to the store...')
        break
    elif answers == 'woods':
        print('Going to the woods...')
        break
#...

Then, your second while statement makes no sense in this way. If you just wanted to print That is not a valid answer in case the input differs from store or woods and give a user another try - then you can just use else, without lists at all:

print('Do you want to go to the store or woods?')

# no lists
while True:
    answers = input()
    if answers == 'store':
        print('Going to the store...')
        break
    elif answers == 'woods':
        print('Going to the woods...')
        break
    else:
        print('That is not a valid answer')

If you instead would like to check, whether the user's input is encountered in lists, then you need to do this in trick inside out:

print('Do you want to go to the store or woods?')

lists = ('woods', 'store')
while True:
    answers = input()
    if answers == 'store':
        print('Going to the store...')
        break
    elif answers == 'woods':
        print('Going to the woods...')
        break
    elif answers not in lists:
        print('That is not a valid answer')
    else:
        # some default case, for example sys.exit() (needs sys to be imported)
  • 1
    @C9Fox I'm happy this helped you! But next time [before creating a question] please consider investigating into your code a bit - for example, read about `break` statements and how do they work, as well as `while` and `continue`. –  Aug 17 '16 at 16:42
  • I will try but I find it so hard to find information about breaks and loops etc, since they are used in a different way in a different code... – C9 Fox Aug 17 '16 at 17:30
  • 1
    Well, there are TONS of information about such a common topic. Moreover, all C-like languages implement this. Just find any Python book for beginners and start reading. –  Aug 17 '16 at 17:32
  • I am very new to coding, started for 3 months ago. I have read automate the boring stuff with python, and done codecademy's python course + treehouse. I am still feeling very fresh it feels like. But maybe i should read another book. – C9 Fox Aug 17 '16 at 17:50