-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
  • 8,084
  • 8
  • 48
  • 62
  • 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

2 Answers2

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
  • 14,275
  • 14
  • 73
  • 117
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
  • 5,337
  • 3
  • 18
  • 35