thefence2113
thefence2113

Reputation: 145

Ending loop for an appended list

I need to create a program that asks for users names in an appended list and then ends when the user types "q:. I have created the code to ask for names but am having trouble with where to end it. My loop is not breaking when I think it should. It keeps running

I have tried making it a for loop and a while loop and have had more success with a while loop but I could be incorrect.

# names of people that I know
names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    names.append(input("Please enter your first name, "
                       "type q to exit: ", ))
    print(names)
    if names == "q":
        break

I expect the results to be:

Please enter your first name, type q to exit: jon
['Billy', 'Trevor', 'Rachel', 'Victoria', 'Steve', 'jon']
Please enter your first name, type q to exit: quit
['Billy', 'Trevor', 'Rachel', 'Victoria', 'Steve', 'jon', 'quit']

Upvotes: 3

Views: 913

Answers (5)

perennial_noob
perennial_noob

Reputation: 468

The problem is that you are already appending to the list before you do your check to see if it is 'q' or not. Also, from your question you want to quit on 'q' only. Not 'quit' or 'q'. If you want to check for 'quit' too then you should add that to your if condition.

Also, do this check and only append if it is not your exit condition. So I suggest this:

# names of people that I know
names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    name = input("Please enter your first name, type q to exit: ")
    if name == "q" or name == "quit":
        break
    names.append(name)
print(names)

If you want to stick to your approach then before breaking you want to remove the last element because that would be a 'q' or a 'quit'. So:

names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    names.append(input("Please enter your first name, "
                   "type q to exit: ", ))
    print(names) #Printing here will still show you the name 'q' or 'quit'. Move this down too.
    if names[-1] == 'q' or names[-1] == 'quit':
        names.pop() #This will remove the 'q' or 'quit'
        break

Upvotes: 1

ksholla20
ksholla20

Reputation: 168

You are comparing the list with string 'q', which will be false always. You can modify

while True:
    inp = input("Please enter your first name, "
                   "type q to exit: ", )
    names.append(inp)
    print(names)
    if inp == "q":
        break

Upvotes: 0

nathancy
nathancy

Reputation: 46630

You have the right idea. But once you get a name from input() you immediately insert it into your list before checking if you want to quit the program. So the solution is to check for the quit signal then append the name.

# names of people that I know
names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    name = input("Please enter your first name, "
                       "type q to exit: ", )
    if name == "q":
        break
    else:
        names.append(name)
print(names)

Upvotes: 1

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

You should take your input in another variable called name and break the loop when you see q. Right now you are directly appending the input to names list

names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    name = input("Please enter your first name, "
                       "type q to exit: ", )
    if name == "q":
        break
    names.append(name)
    print(names)

Upvotes: 2

AlterV
AlterV

Reputation: 301

You are comparing the whole list, names to "q" when checking to exit. Instead, you want to check if the most recent input was "q".

You can do this by checking the last element in the list, e.g. by changing your condition to

if names[-1] == "q"

The rest of the code can be left the same.

Upvotes: 0

Related Questions