Python Forum
simple list check with loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple list check with loops
#1
Huh
Now for the purpose of knowledge and learning could you iterate on simple loop / list basics. I'm using PyCharm IDE, python v 3.5 if that makes any difference.

I would like to understand how to get a simple loop that checks the usernames and continues to check them until a username thats different from one written in the list is entered. I need to understand what is happening and why I'm having such different results with the variations I've tried. I have tried several different loops and when testing some work to stop a repeated username for the first iteration but lets it slide in the next iteration or there is a matter where it will stop a repeat of the same username being entered in several times but when a different username thats still in the list is use it will let it pass. Almost like whats being entered into username input is giving n[0] the value of username and at the same time checking the other parts of n[0]. I don't really know how to eplain it so I'll jot down the loop / list that I need to understand. If anyone could help me understand what's happening and on a basic level consisting of loop / lists basics how do I fix the issue and why that solution would work.


username = input("Enter a username: ")

usrdatalist = [['josh' , 'mi'], ['ryan' , 'th'], ['loki' , 'ch']]

# Create loop to check username
for n in usrdatalist:
    while (n[0] == username):
        username = input("Name taken, enter new name: ")
    if (n[0] != username):
        print("Account created!")
Output:
/usr/bin/python3.5 /root/PythonProjects/video_game/looplistcheck.py Enter a username: josh Name taken, enter new name: ryan Account created! Name taken, enter new name: loki Account created! Name taken, enter new name: josh Account created! Process finished with exit code 0
Reply
#2
This looks like the previous post
Reply
#3
Try this, it might help you understand what's hapening in your code: http://pythontutor.com/visualize.html#co...nces=false

If you're having trouble with loops in general, I'd recommend looking up nedbat's "Loop like a native"
Reply
#4
Notwithstanding the obvious need to learn about loops and how to use properly, you should know that you don't need to loop over the list elements to check that something belongs to the list. Here is hint:
Reply
#5
(Jan-05-2017, 06:53 PM)buran Wrote: Notwithstanding the obvious need to learn about loops and how to use properly, you should know that you don't need to loop over the list elements to check that something belongs to the list. Here is hint:

Sweet thanks for the hint :D. Would it check the list everytime username is input? I will write it out and test. I know this post is similar to the last one I posted but not a dupe post. In the previous post I asked for help fixing a registration form. In the post I'm trying to gain knowledge on basics in python because I just started programming 2 months ago and I've researched that python is a good starting language. If anyone could explain in detail whats going on in this loop I'd really appreciate that. I will do my research however I also need some iteration on this specific loop if you someone wouldn't mind. I will give back to the community. I believe I have the capabilities to become a great asset in computer science. I do these side projects fully non-profit and self-motivated.

I don't know who made http://pythontutor.com/visualize.html#co...nces=false but I love them. So basically usrdatalist gets broken down into however many username, password combanations exist and saved as their own lists. When the for loop is ran n[0] checks the first sublist, then when the for loop ends it jumps into the next sublist until all sublists have been ran through but never checking all the sublists every time the loop is ran and or never going back to previous sublists to check the previous usernames after jumping. How would I write the code to check every sublist? Using a loop or not?

This is indeed incorrect syntax, if you wouldn't mind helping me fix it so I can run it in pythonvisualtutor


username in usrdatalist:
    if username[0] == username:
        username = input("Name taken, enter new name: ")
Reply
#6
(Jan-06-2017, 04:48 AM)Low_Ki_ Wrote: This is indeed incorrect syntax, if you wouldn't mind helping me fix it so I can run it in pythonvisualtutor

if username in usrdatalist:
    username = input("Name taken, enter new name: ")
Or maybe even:

while username in usrdatalist:
   username = input("Name taken, enter new name: ")
print("Account created!")
Reply
#7
Awesomeness! Thank you folks. All of you have been very helpful.
Reply
#8
Okay, I ran this code in pythontutor and it seems that it actually never checks the usrdatalist for the matching usernames. gahh.

Python 3.3
1    username = input("Enter a username: ")
2    
3    usrdatalist = [['josh' , 'mi'], ['ryan' , 'th'], ['loki' , 'ch']]
4    
5    # Create loop to check username
6    while username in usrdatalist:
7        if username == username[0]:
8            username = input("Name taken, enter new name: ")
I also tested this, and found that is still doesn't check the username match, is it because it's reading in the letters in seperate strings. Indentation is required in order to read in the full name for some reason.

username = input("Enter a username: ")

usrdatalist = [['josh' , 'mi'], ['ryan' , 'th'], ['loki' , 'ch']]

# Create loop to check username
while username[:] in usrdatalist:
    username = input("Name taken, enter new name: ")
print(username[:])
After testing these and then figuring out what was happening with your help I've solved my problem and now have a better understanding of how loops work:
Here is the solution in case anyone was wondering.

username = input("Enter a username: ")

usrdatalist = [['josh' , 'mi'], ['ryan' , 'th'], ['loki' , 'ch']]

# Create loop to check username
namestaken = []
for n in usrdatalist:
    namestaken.append(n[0])

while username in namestaken:
    username = input("Name taken, enter new name: ")
I'm sure you good folks will be hearing a lot from me. I do apologize if I've been quite annoying. Your help is greatly appreciated as you know. Thank you guys.
Reply
#9
note that in first two snippets (apart from any other mistakes) you use username to hold the user input and to iterate over list elements. So you overwrite the user input.
Here is refined version using list comprehension of your last code snippet
username = input("Enter a username: ")
usrdatalist = [['josh' , 'mi'], ['ryan' , 'th'], ['loki' , 'ch']]
namestaken = [usr[0] for usr in usrdatalist]
while username in namestaken:
    username = input("Name taken, enter new name: ")
Also, it's a good idea to use dict to store username/password pairs
username = input("Enter a username: ")
usrdatalist = {'josh':'mi', 'ryan':'th', 'loki':'ch'}
while username in usrdatalist:
    username = input("Name taken, enter new name: ")
Reply
#10
(Jan-06-2017, 06:47 PM)buran Wrote: note that in first two snippets (apart from any other mistakes) you use username to hold the user input and to iterate over list elements. So you overwrite the user input.
Here is refined version using list comprehension of your last code snippet
username = input("Enter a username: ")
usrdatalist = [['josh' , 'mi'], ['ryan' , 'th'], ['loki' , 'ch']]
namestaken = [usr[0] for usr in usrdatalist]
while username in namestaken:
    username = input("Name taken, enter new name: ")
Also, it's a good idea to use dict to store username/password pairs
username = input("Enter a username: ")
usrdatalist = {'josh':'mi', 'ryan':'th', 'loki':'ch'}
while username in usrdatalist:
    username = input("Name taken, enter new name: ")

Awesome, I do understand now. I like the refined version of the code but could you explain how dict is different than list? How do the pairs in dict differ from the pairs in lists? All three ways produce the same output when tested. Of course if there are more refined / better ways to write a code, it should be used. Although I would like to understand more on this topic dict vs list and how could it be beneficial when matching usernames and passwords after registration for login. From my understanding matching usernames and passwords in separate lists is just a matter of finding the placement of the input username and placement of the input password and comparing the placement of the two. So if username is in usernames[0] and password is in passwords[0] then it is a match. Is this the same for dict? Also how would I open a file to read from and append to a dict rather than a list.

usrdatalist = []

# Open file to read
with open("usrdata", "r") as usrinfo:
    for l in usrinfo:
        un, pw = l.split(',')
        usrdatalist.append([un,pw])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 422 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 724 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  for loops break when I call the list I'm looping through Radical 4 912 Sep-18-2023, 07:52 AM
Last Post: buran
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,247 Jun-14-2022, 08:35 PM
Last Post: thesquid
  check if element is in a list in a dictionary value ambrozote 4 1,992 May-11-2022, 06:05 PM
Last Post: deanhystad
  How to check if a list is in another list finndude 4 1,848 Jan-17-2022, 05:04 PM
Last Post: bowlofred
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,520 Aug-12-2021, 04:25 PM
Last Post: palladium
  Using recursion instead of for loops / list comprehension Drone4four 4 3,162 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  how to check if string contains ALL words from the list? zarize 6 7,262 Jul-22-2020, 07:04 PM
Last Post: zarize
  Creating a List with many variables in a simple way donnertrud 1 2,048 Jan-11-2020, 03:00 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020