简体   繁体   English

Else 语句继续执行 PYTHON

[英]Else statement keeps executing PYTHON

This functions searches for records in my database.此函数在我的数据库中搜索记录。 Apart from if condition the else statement ( "Record not found") also keeps on executing even if the condition is true.除了 if 条件之外,即使条件为真,else 语句(“找不到记录”)也会继续执行。 OUTPUT screenshot OUTPUT 截图

def displaySearchAcc():
try:
    command = "SELECT * FROM BANK"
    mycursor.execute(command)
    s = mycursor.fetchall()
    ch = input("Enter the account number to be searched : ")
    for i in s:
        i = list(i)
        if i[0] == ch:
            print("*" * 125)
            print("ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE")
            print("=" * 125)
            for j in i:
                print("%14s" % j, end=' ')
                print()
        else:
            print("record not found")
except:
    print("Table not found")
def displaySearchAcc():
try:
    command = "SELECT * FROM BANK"
    mycursor.execute(command)
    s = mycursor.fetchall()
    ch = input("Enter the account number to be searched : ")
    for i in s:
        if i[0] == ch:
            print("*" * 125)
            print("ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE")
            print("=" * 125)
            for j in i:
                print("%14s" % j, end=' ')
                print()
        else:
            print("record not found")
except:
    print("Table not found")

You don't even need the for loop.你甚至不需要 for 循环。 You can perform the search just by using SQL statements which is way faster than the iterative method you are using.您只需使用 SQL 语句即可执行搜索,这比您使用的迭代方法要快得多。 Assuming the column name is account_number假设列名是 account_number

def displaySearchAcc():
    account_no = input("Enter the account number to be searched : ")
    command = f"SELECT * FROM BANK WHERE account_number = '{account_no}'"
    row = mycursor.fetchone()
    if row is not None:
        print("*" * 125)
        print(
            "ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE"
        )
        print("=" * 125)
        for col in row:
            print("s" % col, end=" ")
            print()
    else:
        print("Record not found")

If you really insist on using the iterative method, then the mistake you were making was the line i = list(i) .如果您真的坚持使用迭代方法,那么您所犯的错误就是i = list(i)行。 i is already a list and by putting i in list you are making list of lists (2D list) which messes up your iteration. i已经是一个列表,通过将 i 放入列表中,您正在制作列表列表(2D 列表),这会扰乱您的迭代。

def displaySearchAcc():
    try:
        command = "SELECT * FROM BANK"
        mycursor.execute(command)
        all_rows = mycursor.fetchall()
        account_no = input("Enter the account number to be searched : ")
        for row in all_rows:
            if row[0] == account_no:
                print("*" * 125)
                print("ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE")
                print("=" * 125)
                for col in row:
                    print("%14s" % col, end=' ')
                    print()
                break; # No need to iterate further
        else:
            print("record not found")
    except:
        print("Table not found")

Also, use better variable names.此外,使用更好的变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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