Hamza Ebrahim
Hamza Ebrahim

Reputation: 27

User Input being incorrectly appended to Python List

Python newbie here.

My program prompts the user for their name. There is only one "correct" name, which is John. When the user inputs an incorrect name (ie. not John), the program adds the incorrect name to a list. Once the correct name, John is entered I would like to print the list of incorrect names.

The problem that I am encountering/do not understand is that when John is entered it has been appended to the list of incorrect names. Below is my code:

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    incorrectNames.append(name)
    if name == "John":
        print("Incorrect names", incorrectNames)

This code returns [Andy, Sam, John] instead of just [Andy, Sam] (if those were the two names entered incorrectly before John). Is this due to an order of operations? I thought that once the variable name was John that would circumvent the append instruction?

Any sort of clarity would be appreciated or pointing me in the right direction.

Thanks in advance!

Upvotes: 0

Views: 558

Answers (4)

MarkH
MarkH

Reputation: 31

You are inside the while loop (the next iteration) when John is entered. Next you append John to the list then test for name John. If you test for John before the append you will get the result you want:

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    if name == "John":
        print("Incorrect names", incorrectNames)
    incorrectNames.append(name)

Upvotes: 0

Preston Martin
Preston Martin

Reputation: 2963

Pretty simple actually. You are appending the name to the list regardless of whether or not the name is equal to "John". To solve this, simply add an else statement following your if statement.

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    if name == "John":
        print("Incorrect names", incorrectNames)
    else:
        incorrectNames.append(name)

Note: You may want to add a break statement to tell the program to exit your loop, otherwise your application will never exit.

Upvotes: 0

Rajarshi Bandopadhyay
Rajarshi Bandopadhyay

Reputation: 25

I will provide a corrected code below, then give an explanation:

The Code:

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    if name != "John":
        incorrectNames.append(name)
    if name == "John":
        print("Incorrect names", incorrectNames)

Explanation:
Try to picture what is happening as you run the code. Lets say we follow both the value of the variable name and the list incorrectNames after each input from the user.

After Andy is entered, the value of name becomes 'Andy' due to the input statement. Now, note that immediately following the input statement is the line incorrectNames.append(name). This causes the list incorrectNames to become ['Andy'].

After this, the same procedure happens with 'Sam'. First it enters name, then it is appended to the list, making incorrecNames become ['Andy','Sam'].

And then the final loop, when the real action happens.

Now the user enters 'John'. But here lies the problem: immediately following the input statement is the line incorrectNames.append(name). This means that even without checking the value of name, it is appended to incorrectNames.

All you have to do is insert some code to check the value of name, and not append it to incorrecNames if it is John.

Upvotes: 0

abarnert
abarnert

Reputation: 365657

Python executes your instructions in the order you give them. It’s like a recipe—if you write “Put the cake in the oven” as step 1 and “Preheat the oven” as step 2, you’re going to be putting the cake in a cold oven.

So, you can fix it like this:

if name == "John":
    print("Incorrect names", incorrectNames)
incorrectNames.append(name)

However, this is still going to add John to the list. It’ll add him after you print the list, so it won’t show up in the output, but still, you probably don’t want him added at all. So a better answer may be:

if name == "John":
    print("Incorrect names", incorrectNames)
else:
    incorrectNames.append(name)

Or, alternatively, it may be easier to think of printing the names as something you do after the loop, instead of conditionally inside the loop:

while name != "John":
    name = input("Enter your name: \n")
    if name != "John":
        incorrectNames.append(name)

print("Incorrect names", incorrectNames)

And at this point, you may decide that double check is a bit redundant and do this:

incorrect_names = []

while True:
    name = input("Enter your name: \n")
    if name == "John":
        break
    incorrectNames.append(name)

print("Incorrect names", incorrectNames)

This recipe-style sequencing is the way most programming languages work, and it’s called “imperative” execution. Some “functional” languages avoid this confusion by just not letting you write multiple instructions at all; you have to explicitly say “this function composes these other two functions” or similar. Other “dataflow” and “logic” languages work more like a spreadsheet, where you tell it each how each variable depends on each other variable and then let it figure out the sequence to get the value of whichever variable you ask for. But generally, languages that let you write a bunch of instructions in sequence execute them imperatively, recipe-style, just like Python.

Upvotes: 1

Related Questions