Make a single string to be a part of list

can you guy help me.. I try to find how to make a single string into a list.

The question is like this:

The validate_users function is used by the system to check if a list of users is valid or invalid. A valid user is one that is at least 3 characters long. For example, ['taylor', 'luisa', 'jamaal'] are all valid users. When calling it like in this example, something is not right. Can you figure out what to fix?

 def validate_users(users):
      for user in users:
        if is_valid(user):
          print(user + " is valid")
        else:
          print(user + " is invalid")
    
    validate_users("purplecat")

I tried but still do not get the right outcome,

purplecat is valid

the outcome should be like that but still get in like

p is invalid
u is invalid
r is invalid
p is invalid
l is invalid
e is invalid
c is invalid
a is invalid
t is invalid

can someone explain?

Upvotes: 0

Views: 5519

Answers (9)

AuroSilalahi
AuroSilalahi

Reputation: 1

def validate_users(users):
  for user in [users]:
    if len(user) > 3:
      print(user + " is valid")
    else:
      print(user + " is invalid")

validate_users("purplecat")

we can use "[]" at the range on it. Hope it works and add the length of char from user to make sure the if it's right

Upvotes: 0

Ramdev CM
Ramdev CM

Reputation: 15

def validate_users(users):   # takes list as parameter
  for user in users:         # iterates for users[0]: ["purplecat"]
    if is_valid(user):
      print(user + " is valid")
    else:
      print(user + " is invalid")

validate_users(["purplecat"]) # make this as a list

Upvotes: 0

Ramdev CM
Ramdev CM

Reputation: 15

def is_valid(user):
  if len(user) >=3 :
    return 1
  return 0

def validate_users(users):
    if is_valid(users):
      print(users + " is valid")
    else:
      print(users + " is invalid")

validate_users("purplecat")

Upvotes: 0

Guru
Guru

Reputation: 1

def validate_users(users):
  for user in users:
    if is_valid(user):
      print(user + " is valid")
    else:
      print(user + " is invalid")

validate_users(["purplecat"])

Upvotes: 0

Marc Riley
Marc Riley

Reputation: 1

def validate_users(users):
  for user in users:
    if len(user) > 3:
      print(user + " is valid")
    else:
      print(user + " is invalid")

validate_users(['purplecat'])

Here is your output:

purplecat is valid

Upvotes: -1

alani
alani

Reputation: 13079

As others have pointed out, if you want to make a one-element list containing a string, you would do it using [my_string] where my_string is the string object e.g. "purplecat".

There are a couple of alternatives, which would allow your function to act more gracefully if a string is passed in the users parameter.

One option is to accept a string as an alternative to a list, and reassign the users variable to a one-element list if it is not already a list:

def validate_users(users):
    if not isinstance(users, list):
        users = [users]
    # then carry on as before

And another alternative would be to fail in a more predictable way:

def validate_users(users):
    if not isinstance(users, list):
        raise ValueError("users must be a list")
    # then carry on as before

You can then write the rest of the function without having to worry about whether you have been passed something other than a list. In the latter case, it up to the caller to ensure that it passes in a list or deals with any ValueError.


For simplicity I am not considering tuples here, although you would probably want to accept a tuple as an alternative to a list, so for example instead of

    if not isinstance(users, list):

you could have

    if not isinstance(users, (list, tuple)):

Upvotes: 1

YeonwooSung
YeonwooSung

Reputation: 61

In this example, you pass the string "purplecat" as an argument of the function validate_users. So, users = "purplecat"

And in the validate_users function, you used the for loop which iterates the argument users. In python, if you use for loop for the string type variable, then the for loop will iterate all characters in the string.

If you want to get an output like "purplecat is valid", then you need to use "validate_users(["purplecat"])".

In this case, you pass a list of string, so the for loop in the validate_users will iterate all strings (in this case it will iterate only once since the list only has 1 element) in the list.

Upvotes: 1

orestisf
orestisf

Reputation: 1472

The problem is that you are iterating over a string here: for user in users but users == "purplecat".

Try calling your function like that: validate_users(["purplecat"])

Upvotes: 3

Chris Schmitz
Chris Schmitz

Reputation: 658

You're passing 'purplecat' as a string so when the loop goes through, it considers each element of the string as one thing to iterate through. Try putting it into a one-element list:

validate_users(['purplecat'])

Upvotes: 6

Related Questions