-2

So i feel pretty stupid but I've been stuck on this task forever and I honestly don't know how to fix it or what's wrong with it. I've changed it so much in the process of trial and error I think only errors are left and I'm more confused now than when I started. So I'm supposed to get a user's input and as long as the input isn't equal to 'John', it should keep asking, if it is, it should stop the loop and print out all incorrect input. But I'M STUCK. Please be patient and help this idiot...i know I didn't define a list because I don't know how I should in this example. what do i do next?

name='John'
your_name=''
while (your_name!= name):
   your_name=(raw_input("Enter your name?"))
if your_name==name:
ElleO
  • 1
  • 2
  • 1
    Your code does not do anything after you exit the `while`-loop but you want to print something. And what is `alphabet`? – Psytho Dec 22 '15 at 11:48
  • 1
    @timgeb This is not a duplicate, because ElleO knows how to ask for input until getting a valid response. See the code. – Psytho Dec 22 '15 at 11:50
  • All this results in is `IndentationError: expected an indented block` on the last line (not complete if statement). After removing that the code seems to work correctly (except it doesn't print out the incorrect names). – skyking Dec 22 '15 at 12:22

2 Answers2

1

The if inside the loop is wrong: you should break out of the loop if equality is true and store the bad name to the incorrectNames list if not:

while (your_name!= name):
    your_name=(raw_input("That's not the name I'm looking for,try again"))
    if your_name==name:
        break
    else:
        incorrectNames.append(your_name)
    # print alphabet['']
print "Incorrect names so far : " + ', '.join(incorrectNames)
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

A few (deliberately minimal) tweaks results in the following:

name = 'John'
incorrect_names = []
your_name = raw_input("What is your name?")
while your_name!= name:
    incorrect_names.append(your_name)
    your_name = raw_input("That's not the name I'm looking for,try again")

# print out incorrect names, if any
for name in incorrect_names:
    print name
mhawke
  • 84,695
  • 9
  • 117
  • 138