0

I don't know how to make the script keep asking for the pet's name if I enter a name that is not in the myPets list. So if I type in Tiny ( a name not in the list) the script runs and closes after it prints out ('I do not have a pet named '+ name) 'I do not have a pet named Tiny.

I want to put a for loop in the script so it will ask me again for an input.

myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter Pets Name:')
name = input()
if name not in myPets:
    print('I do not have a pet named '+ name)
else:
    print(name + ' is my pet.')
  • What is the rule that will tell you when the loop should stop? If you know that there is such a thing as a "for loop", do you know what it looks like? If you know what it looks like, then what is preventing you from figuring out where to put it? – Karl Knechtel May 14 '20 at 03:59
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – rdas May 14 '20 at 03:59
  • Anyway, a question like this is better answered by trying to follow along with a tutorial, and then perhaps you can ask a specific question about something the tutorial advised you to do that confused you. – Karl Knechtel May 14 '20 at 04:00

5 Answers5

0

Not sure about using a for loop, but you can definitely use an infinite while loop to make this happen:

myPets = ['Zophie', 'Pooka', 'Fat-tail']
while True:
    print('Enter Pets Name:')
    name = input()
    if name not in myPets:
        print('I do not have a pet named '+ name)
    else:
        print(name + ' is my pet.')
        break
Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
  • 1
    This was so good and what I wanted - but to make it act more in line with something else I was seeking was to take break statement out. Thank you very much – Peter Cockram May 14 '20 at 19:23
  • Good to know... Please upvote the answer if it helped! – Prateek Dewan May 14 '20 at 19:52
  • After looking with all the great answers you really did a simple "answer the question" job. I do need to remove the break to have it loop. I have to ask - how would I call "break" if I needed to? Good Job! – Peter Cockram May 14 '20 at 20:06
  • An infinite loop like that, without the break statement, can be terminated with a Ctrl+C keyboard interrupt. Is that what you need? – Prateek Dewan May 14 '20 at 20:55
0

You might want to use while loop here so that you can continue to ask for the correct input

0

It's a little elementary, but you can just replace your if with a do-while construct like so:

myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter Pets Name:')
name = input() 
while name not in myPets: 
    print('I do not have a pet named '+ name)
    name = input ()
print(name + ' is my pet.')
Azmat
  • 97
  • 1
  • 11
  • I don't understand why you think this doesn't work, and `not name in myPets` doesn't make any sense (how do you negate a string?). I've tested this in a repl and verified it works: https://repl.it/join/dnvvbukb-azmathussain – Azmat May 14 '20 at 14:44
  • Thanks - I wanted the script to keep running even after a name in the list was entered by the user. Looks like I needed a while loop and not for loop. Thanks – Peter Cockram May 14 '20 at 19:26
  • I see, your description of "keep asking for the pet's name if I enter a name that is not in the myPets list" lead me to believe you wanted to terminate the loop after valid entry. – Azmat May 14 '20 at 19:28
0

Try with the while loop.


myPets = ["Zophie", "Pooka", "Fat-tail"]
def test():
    while True:
        name = input("Enter the pet name: ")
        if name in myPets:
            print(name + " is my pet.")
            test()
        else:
            print("I do not have a pet named " + name)
            break

test()
chatrapathi
  • 107
  • 8
0

Hello Pete,

Thank you for your question. Please have a look at the code below where I try to answer it as much as possible. There are comments in every line explaining what we are doing along with the respective results. I certainly hope this helps, buddy. Keep coding in Python, this is a great and fun language to learn. I am also learning it right at this moment.

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

#Let us start by getting the Pet's name on this sentence
myPetName= input("What is your Pet's Name?\n")

#As long as the user keeps  typing the wrong name, the
# routine will keep asking for your pet's name,
# for the example we will use the name "Zophie"
while myPetName != 'Zophie':
    print(f"I do not have a pet named {myPetName}.")
    #Now, let's ask one more time your pet's name.   
    myPetName= input("What is your Pet's Name?\n")

# Now that the user types in the right name of your pet
# in this case we are using Zophie, let's print it on the screen
print(f"{myPetName} is certainly the name of my pet.")


Alvison Hunter
  • 620
  • 5
  • 13
  • Thank you I needed a while loop. ``` I just don't know why or what the "f" does in print(f"I do not have a pet named {myPetName}.") ``` Nor do I understand the use of {something} curly brackets - I will understand but not yet. Thank You. You killed it, Sir. – Peter Cockram May 14 '20 at 19:20
  • Sure thingy @PeterCockram, We are honored to serve you! – Alvison Hunter May 14 '20 at 19:21
  • In regards to the print f and the brackets, these are called f-srings or formatted string literals, it is a more concise yet precise way of printing on the screen without a wide range of options. I'm sharing with you more information about the vast amount of options that you have with this. https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/ – Alvison Hunter May 14 '20 at 19:24