Home > other >  Python validation when utilizing .append
Python validation when utilizing .append

Time:12-08

I am unsure how to add validation to my input, specifically a presence check, which looks like this:

team1.append(input("Please enter the name of a team member: "))

Normally with more traditional inputs, my validation would look like this:

exampleInput=input("enter input")
while exampleInput=="":
exampleInput=input("enter input")

But that doesn't seem to work, as the validation does nothing if I try something like this:

team1.append(input("Please enter the name of a team member: "))
while team1.append(input)=="":
team1.append(input("Please enter the name of a team member: "))

I'm not sure how I could add any sort of validation to this. Any ideas?

CodePudding user response:

Something like this?:

inp = input("Please enter the name of a team member: ")
if inp != "":
    team1.append(inp)

CodePudding user response:

It is not clear to me what you want to achieve: team implies that you want to append multiple members, ignore empty lines, and have a way to exit the loop: the logic presented so far will stop once one valid member (i.e. not empty space) is entered, hence you'll get a list of size 1.
For example, I think this would work for that purpose:

team1 = []
this_member = ""
while True:
    this_member = input("Please enter the name of a team member: ").strip()
    if this_member != "":
        if this_member.upper() == "EXIT":
            break
        team1.append(this_member)
    
print(team1)

So an interaction would be:

Please enter the name of a team member: Paul
Please enter the name of a team member: George
Please enter the name of a team member: 
Please enter the name of a team member: 
Please enter the name of a team member: Alan
Please enter the name of a team member: EXIT
['Paul', 'George', 'Alan']
  • Related