0
    available_fruits = ['apple', 'grape', 'banana']
    
    choice = input('Please pick a fruit I have available: ')
    
    while True : choice= input('You made an invalid choice. Please pick again:') 
            if choice not in available_fruits 
                 else print('You can have the fruit')

I want to make this loop stop when user inputs something in the list "available_fruits". But when I enter 'apple', it says "You can have the fruit" but as well as "You made an invalid choice. Please pick again:" again.

How can I make this loop to stop when I enter the fruit which is in the list? when I put the "break" at the end of the code, it says its an invalid syntax..

1
  • 2
    That code is not (valid) Python, where indentation matters. Please extract a minimal reproducible example and provide that as part of your question, so people don't have to guess what the code is that causes problems. As a new user, also take the tour and read How to Ask. Commented Apr 6, 2021 at 19:01

4 Answers 4

3

Use the while condition instead of just while True, something like this works:

available_fruits = ['apple', 'grape', 'banana']

choice = input('Please pick a fruit I have available: ')

while choice not in available_fruits: 
    choice= input('You made an invalid choice. Please pick again:')

print('You can have the fruit')
Sign up to request clarification or add additional context in comments.

Comments

2

This would work:

available_fruits = ['apple', 'grape', 'banana']

choice = input('Please pick a fruit I have available: ')

while True:
    if choice in available_fruits:
        print('You can have the fruit')
        break
    else:
        choice = input('You made an invalid choice. Please pick again: ')

But in general I'd suggest you create while loops with an actual condition. That's what while-loops are for. They automatically break when the condition evaluates to False. It's much cleaner, too.

Example:

available_fruits = ['apple', 'grape', 'banana']

choice = input('Please pick a fruit I have available: ')

while choice not in available_fruits:
    choice = input('You made an invalid choice. Please pick again: ')

print('You can have the fruit')

2 Comments

This still complains about making an invalid choice on the first iteration of the loop.
thank you, you're right :D I edited the code.
1

Give this a try. Assuming you need to use break.

available_fruits = ['apple', 'grape', 'banana']

choice = input('choose a fruit: ')
while True : 
    if choice in available_fruits:
        print('you picked a valid fruit')
        break
    else:
        choice = input('choose a fruit: ')

Comments

1

I don't know if its the formatting of your question of if that's your actual code. You some have syntax errors:

  • At the end of every if or else you need to use : as you did with the while condition.
  • You must use the correct indentation.

Once you fix those errors, check the condition you want to meet in order to get out of the loop with 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.