mercredi 28 octobre 2015

How loop an input over until correct answer is entered

I am finding it difficult to loop my raw_input over and over again until the right answer is entered (Im a noob)

here is the question:

write a Python program that takes in a user input as a String. While the String is not “John”, add every string entered to a list until “John” is entered. Then print out the list.

Example program run (what should show up in the python console when you run it):

Enter your name : <user enters Tim>
Enter your name : <user enters Mark>
Enter your name: <user enters John>
Incorrect names: [‘Tim’, ‘Mark’]

and this is my code:

answer = "John"

nameString = ['']

nameInput = raw_input("Enter a name")

if nameInput in answer:

    print nameString

else:

    nameString.append(nameInput)

I'm not entirely sure what code should be written to achieve this loop.

Aucun commentaire:

Enregistrer un commentaire