1

New to python and having which is probably a basic issue when trying to get a loop to work.

I have read some previous questions similar but couldn't find a solution that works.

I'm just trying to get the same question asked in the script until a listed cat name is mentioned. So If a enter a name like 'Scott' which is not in the list of pets it will ask to try again for a pet name again.

myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter a pet name.')
name = input()
if name not in myPets:
    print('I do not have a pet named ' + name + ' try again')

else:
    print(name + ' is my pet.')
3
  • 1
    change the if to while, and remove the else de-indenting the last print(). Finally, update the name inside the while with another input() Commented Jun 21, 2017 at 11:02
  • You can also use raw_input() so that you don't have to wrap your input with " marks always ;) Commented Jun 21, 2017 at 11:15
  • Thanks EV. I tried with your comments, this did loop and ask for another name but when I entered a valid name it still asked for another pets name instead of ending.... or maybe I didnt make correct changes. Commented Jun 21, 2017 at 11:17

5 Answers 5

4

You can use a while loop to repeat until the user enters a correct input. Use a break to exit from the loop.

myPets = ['Zophie', 'Pooka', 'Fat-tail']
while True:
    print('Enter a pet name.')
    name = input()
    if name not in myPets:
        print('I do not have a pet named ' + name + ' try again')
    else:
        print(name + ' is my pet.')
        break
Sign up to request clarification or add additional context in comments.

Comments

3

For this task you should use a while loop like this :

myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter a pet name.')
name = input()
while name not in myPets:
    print('Enter a valid pet name.')
    name = input()
print(name + ' is my pet.')

Each time the user enters something, the condition is evaluated. If your condition is correct, you'll keep asking for another input from the user until it matches your requirements.

Comments

2

while is the keyword you need. use while loop
It helps you repeat a set of statements while a condition is satisfied (i.e. until something new happens e.g entered name is one of your pets).

You can also pass your input message as an argument to the input() method.

myPets = ['Zophie', 'Pooka', 'Fat-tail']
name = input("Enter pet name")
while name not in myPets:
    print('I do not have a pet named ' + name + ' try again')
    name = input("Enter pet name")
print(name + ' is my pet.')

Comments

1

here is what you are looking for:

myPets = ['Zophie', 'Pooka', 'Fat-tail']

done=Fasle
while not done:
    name=input("enter a pet name: ")
    if name in myPets:
       done=True
       print(name + ' is my pet.')
    else:
        print('I do not have a pet named ' + name + ' try again')

Comments

1

You can use a simple while loop:

myPets = ['Zophie', 'Pooka', 'Fat-tail']
loop=0
while loop==0:
    print('Enter a pet name.')
    name = input()
    if name not in myPets:
        print('I do not have a pet named ' + name + ' try again')

    else:
        print(name + ' is my pet.')
        loop=loop+1

Or:

Use a recursion loop (not recommended):

myPets = ['Zophie', 'Pooka', 'Fat-tail']
def ask():
    print('enter pet name')
    name = input()
    if name not in myPets:
        print('I do not have a pet named ' + name + ' try again')
        ask()

    else:
        print(name + ' is my pet.')

ask()

4 Comments

what you call a 'def: loop' is actually called 'recursion', a function calling itself. Watch the memory usage of your program and also see how many times you can input the wrong answer until you get a "maximum recursion depth exceeded' error.
Now after learning this, I recommend to edit your answer. Just click on edit and change the text from 'Def: loop' to 'recursion'. It's always recommended to improve your answers.
Yeah will do that now
Thanks again btw for the tip, I'm doing my GCSE's at the moment, don't want to make that mistake! @jps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.