'userToList() only return user_list but not the user_id_list
# The main goal is to use other functions
# to add user id and user details to their respective lists
def userToList (user_id_list, user_list):
username = usrInput("letter")
while(checkUname(username, user_list) == True):
print("The username already exists, please enter a different username")
username = usrInput("letter")
passwordE = usrInput("letter_or_number_or_underscore")
password = encryption(passwordE)
email = usrInput("email")
postcode = usrInput("number")
# Adding the inputs to uPEP_List list
uPEP_List = [] # uPEP stands for username Password Email Postcode_list
uPEP_List.append(username)
uPEP_List.append(password)
uPEP_List.append(email)
uPEP_List.append(postcode)
user_list.append(uPEP_List) # add the uPEP_List to user_list
return user_list
# Now, an unique user id will be created based on the postcode the user provided
userId = ""
if 1000 <= int(postcode) < 2000:
userId += randomID(7, user_id_list)
elif 2000 <= int(postcode) < 3000:
userId += randomID(8, user_id_list)
elif 3000 <= int(postcode) < 4000:
userId += randomID(9, user_id_list)
elif 4000 <= int(postcode) < 5000:
userId += randomID(10, user_id_list)
else:
userId += randomID(11, user_id_list)
user_id_list.append(userId)
return user_id_list
users_id_list = ["123456789", "123456779"]
users_list = [["aaaaa", "^^^%1%%%2%%%%%2%%%%2%$$$", "[email protected]", "3131"], ["bbbbb",
"^^^%2%%%2%%%%%3%%%%4%$$$", "[email protected]", "3231"]]
userToList(users_id_list,users_list )
Please give your alphabetical input: etrte
Please give your alphanumeric, including '_', input: 234234
Please give your email input: [email protected]
Please give your numeric input: 3333
The output is:
[['aaaaa', '^^^%1%%%2%%%%%2%%%%2%$$$', '[email protected]', '3131'], ['bbbbb', '^^^%2%%%2%%%%%3%%%%4%$$$', '[email protected]', '3231'], ['etrte', '^^^'2'""3""'''4''''2'""3""'''4'''$$$','[email protected]', '3333']]
As you can see, it only shows the user_list updated list and avoids the user_id_list. I don't what's wrong
Edit:
# In the comments, I meant like this, I will define another function and in that function I will call
#that function's user_list_id and user_list
#The main purpose of this function is to test out the pre- defined functions as a program
def testFunc():
users_id_list = ["123456789", "123456779"]
users_list = [["aaaaa", "^^^%1%%%2%%%%%2%%%%2%$$$", "[email protected]", "3131"], ["bbbbb",
"^^^%2%%%2%%%%%3%%%%4%$$$", "[email protected]", "3231"]]
users_id_list, users_list = userToList(users_id_list, users_list);print(users_id_list, users_list)
testFunc()
Solution 1:[1]
As of right now you have two returns in your function, this is a problem as the code after the first return will not be executed.
You can however just comment out your first return, and then return both the lists of userID and user_list:
def userToList (user_id_list, user_list):
username = usrInput("letter")
while(checkUname(username, user_list) == True):
print("The username already exists, please enter a different username")
username = usrInput("letter")
passwordE = usrInput("letter_or_number_or_underscore")
password = encryption(passwordE)
email = usrInput("email")
postcode = usrInput("number")
# Adding the inputs to uPEP_List list
uPEP_List = [] # uPEP stands for username Password Email Postcode_list
uPEP_List.append(username)
uPEP_List.append(password)
uPEP_List.append(email)
uPEP_List.append(postcode)
user_list.append(uPEP_List) # add the uPEP_List to user_list
#return user_list Do not have this one!
# Now, an unique user id will be created based on the postcode the user provided
userId = ""
if 1000 <= int(postcode) < 2000:
userId += randomID(7, user_id_list)
elif 2000 <= int(postcode) < 3000:
userId += randomID(8, user_id_list)
elif 3000 <= int(postcode) < 4000:
userId += randomID(9, user_id_list)
elif 4000 <= int(postcode) < 5000:
userId += randomID(10, user_id_list)
else:
userId += randomID(11, user_id_list)
user_id_list.append(userId)
return user_id_list, user_list
And you can call the function like this:
user_id_list, user_list = userToList(users_id_list, users_list)
Although you are taking the lists as input and doesn't even have to return them again as lists are mutable in python and will change the original lists to have the new elements within. So another solution would be to remove the returns all togheter for another working solution.
Edit based on comments: To use the lists seperatly and to send to another function:
def another_function(a_list):
for element in a_list:
# DO something with the elements in the list, as assign or something else
print(element)
another_function(user_id_list)
another_function(users_list)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
