-1

Trying out Python here and I have question about the use of functions on lists. I`m currently running into trouble with an assignment to check new usernames against current users. What I want to do is change the values in the list to lower cases so that if a new user "Kim" is not accepted since there is a current user "kim". Here is my code:

new_users.lower() = ["Jeroen","naj","Kim","henk","ayla","nimda"]

current_users.lower() = ["Jeroen","jan","kim","henk","ayla","admin"]

for new_user in new_users:
    if new_user in current_users:
        print("\tHello, " + new_user + ". Please choose a different username")       
   else:
        print("\nHello "+ new_user + " We accept your username. Welcome.")

Unfortunately Python gives me the following error: "can't assign to function call". But I have no idea what Python means with this. Why can`t I use a simple lowercase function on the values in a list before I check them in my for loop? Please explain / help.

Vadim Kotov
  • 7,766
  • 8
  • 46
  • 61
  • 2
    Unfortunately, I think if you are stumped by the fact that you can't assign to a function call, you need more help than we can provide in a short answer. Giving you a oneliner to build a list of lower case strings won't help you. You need to work through a tutorial before you will be able to ask good questions here. – timgeb Sep 04 '17 at 15:11
  • try `new_users = [x.lower() for x in ["Jeroen","naj","Kim","henk","ayla","nimda"]]` – Jean-François Fabre Sep 04 '17 at 15:13
  • Thanks for your help. This is an question from a tutorial after the section that explains if/else functions. Unfortunately I didn`t read or made an exercise explaining the difference of calling a function or making a list. Thanks for the answer and I`ll delve into it a bit more. Hopefully this stuff gets explained. – Jeroen Hofsteenge Sep 05 '17 at 06:49

3 Answers3

0

You have to iterate through the list and convert each string to lower(). You can do this with list comprehension:

new_users = ["Jeroen","naj","Kim","henk","ayla","nimda"]
new_users = [s.lower() for s in new_users]
Mohamed Ali JAMAOUI
  • 13,233
  • 12
  • 67
  • 109
0

Firstly You are assigning value for function call.

new_users.lower() 

is function call.

You can achieve what you what to do like this. lets say the current users are in the following list

current_users = ["Jeroen","jan","kim","henk","ayla","admin"]
current_users_lower=[ x.lower() for x in current_users]

and new user be in the following list

new_users = ["Jeroen","naj","Kim","henk","ayla","nimda"]
new_users_lower=[x.lower() for x in new_users]

you can now do the following

for new_user in new_users_lower:
    if new_user in current_users_lower:
        print("\tHello, " + new_user + ". Please choose a different username")       
   else:
        print("\nHello "+ new_user + " We accept your username. Welcome.")
Mitiku
  • 4,929
  • 3
  • 16
  • 33
0

As @timgeb said, maybe you should go through some python basic tutorial first.

But what you want to do here can be done with a map :

raw_list = ["Paul", "marc", "HenrY"]
list(map(lambda x: x.lower(), raw_list))

It will return:

Out[2]: ['paul', 'marc', 'henry']
RobinFrcd
  • 2,489
  • 2
  • 17
  • 38
  • map/list comprehension is way out of OP's league. He should learn how to write a `for` loop today, that's tough enough for now. The vanilla one. Create empty list and append to it in a loop. – timgeb Sep 04 '17 at 15:19
  • Indeed, but he will hold the cards to achieve _everything_ he wants now! – RobinFrcd Sep 04 '17 at 15:22
  • Thanks for the help. Have not yet come across the mapping in the book but I`ll give it a try :) – Jeroen Hofsteenge Sep 05 '17 at 06:50