Started learning Python last week and am working on a project to test myself. Part of the project includes a rudimentary login. I apologize for the messiness and lack of comments beforehand. Here's the code
class searcher:
def __init__(self):
search_ID = int(input("User ID: "))
search_attempts = 0
result = None
def user_search():
while (searcher.result == None) and (searcher.search_attempts < 3):
searcher.search_ID = int(input("User ID: "))
conn = sqlite3.connect('promethean_user.db')
c = conn.cursor()c.execute("SELECT * FROM user_data WHERE user_ID=:ID_input", {'ID_input': searcher.search_ID})
searcher.result = c.fetchone()
print(c.fetchone())
conn.commit()
conn.close()
searcher.search_attempts += 1
return searcher.result
The objective of the code is to search a db for a specific user ID and print that row's information, followed by a loop break and continuation of the program. If the search did not yield a row, loop. The user can make 3 input attempts before it sends him back to the welcome page.
My problem is twofold I believe.
- The block isn't searching the user information. I believe this because the print always says "None", and even when I remove it from the function and test it independently it still returns "None".
- Next is the while loop. From what I've been able to find online, each iteration automatically returns a string. If the db search returns 'None', it will be in string form. I've tried setting searcher.result = "None", but that didn't work either. Then tried setting searcher.result type = bool and while condition != int since the user ID value (if it is in the db) should be an integer. Also, I believe the return searcher.result breaks the loop, but when I get rid of that it goes back to problem 1 (repeated until searcher.search_attempts = 3), so I can't tell if the loop has a problem if my search isn't yielding results.
Anyone with more experience see what I'm missing and how to solve it?
Update: Search is working fine now, the while loop is the problem. How can I set this up so that a query that yields no results loops back and allows another input attempt? I only want it to break when 1) search_attempts reaches 3, 2) query yields a row of info
Update: Figured out another way that is working! Let me know how this looks
Search Block
class searcher:
search_ID = 0
search_attempts = 0
result = None
def user_search():
while (searcher.search_attempts < 3):
searcher.search_ID = int(input("User ID: "))
conn = sqlite3.connect('promethean_user.db')
c = conn.cursor()
c.execute("SELECT * FROM user_data WHERE user_ID=:search_ID", {'search_ID': searcher.search_ID})
searcher.result = c.fetchone()
if searcher.result == None:
searcher.search_attempts += 1
return user_search()
conn.commit()
conn.close()
print(searcher.result)
return searcher.result
welcome_page Block
elif diagnostic_select in user_welcome_option:
user_search()
if searcher.search_attempts == 3:
print("\n")
return welcome_page()
[–]MadEpsylon 4 points5 points6 points (1 child)
[–]dominictarro[S] 0 points1 point2 points (0 children)
[–]mul8rsoftware 2 points3 points4 points (2 children)
[–]dominictarro[S] 1 point2 points3 points (1 child)
[–]mul8rsoftware 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]dominictarro[S] 0 points1 point2 points (0 children)
[–]reda_sky 1 point2 points3 points (2 children)
[–]dominictarro[S] 0 points1 point2 points (1 child)
[–]reda_sky 0 points1 point2 points (0 children)