简体   繁体   中英

Python while loop user input until result found in sql

I would like for my code to ask the user to enter user ID. If User ID is found in the SQL database, the system would print successful. Otherwise, it would keep asking the user to enter user ID. Right now, if I type a valid user ID first (U0020), the code runs fine.

Problem is, if I type in the wrong user ID first (ewehri), even if I key in a valid user ID afterwards, the output is still "Sorry, no such record in the database. Please try again" and prompts for user input again. I'm not sure how to fix this issue.

First, I define calling sql database to get account number from user ID:

conn=create_connection()
def getacct(userid): 
    query = """\
        select Account_Number
        from User_Account
        where UserID = '{}'
        """ .format(userid)

    return (execute_read_query (conn, query))

This works fine. After which, I do this to put the results into a list:

userid = input("Please enter your user id:")
acct_no = getacct(userid)

accountno = []
for i in acct_no:
    accountno.append(i[0])

Then comes the problematic part. I check if the list [accountno] is empty. If it is empty, that means the User ID is not in database and hence could not return any account number. If it is not empty, then it should print "successful" and exits the loop. However, as mentioned, it would not exit the loop.

def User():
    while accountno:
        print("successful")
        break
    else:
        print ("Sorry, no such record in the database. Please try again")
        userid = input("Please enter your user id:")

I also tried many iterations of this code like placing the "put result in list" part of the code inside def User(), or this:

def User():
    while not accountno:
        print ("Sorry, no such record in the database. Please try again")
        userid = input("Please enter your user id:")
        
        if accountno:
            print ("Successful!")

and really much more but I just can't get the output I need. I'm hoping someone here would know what I did wrong. Thanks for your help!

You can use accountno.pop() . right after the if accountno . By using the pop method instead of del, you can use the value later, eg saveUserID = accountno.pop() . An alternative is to use a break statement right after the print("Successful") part. However, another of your issues is that you don't append the userid to accountno , but use the if to check accountno , not userid for the new input. Finally, you should pass accountno as a function argument to User() and use it as a return type. Here is a better example:

def User(userid):
    while not userid:
        print ("Sorry, no such record in the database. Please try again")
        userid = input("Please enter your user id:")

    print ("Successful!")
    return userid

This way your accountno variable will contain a single successfully entered userid. Your print should come after the while loop since it prints only upon success anyway.

To use then, say something like:

accountno.append(User(accountno))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM