Python 3.x 执行以下代码时,Python输出为空:

Python 3.x 执行以下代码时,Python输出为空:,python-3.x,pyodbc,Python 3.x,Pyodbc,这个项目是一个银行系统。它连接到包含客户详细信息和交易详细信息的在线数据库。但是,当我执行代码时,在python 3.4.0 shell中会得到一个空白输出: import pyodbc cnxn = pyodbc.connect('Driver={SQL Server};' 'Server=***;' 'Database=***;'

这个项目是一个银行系统。它连接到包含客户详细信息和交易详细信息的在线数据库。但是,当我执行代码时,在python 3.4.0 shell中会得到一个空白输出:

import pyodbc

cnxn = pyodbc.connect('Driver={SQL Server};'
                            'Server=***;'
                            'Database=***;'
                            'uid=***;pwd=***')
cursor = cnxn.cursor()

def MainMenu():
    print('##############################\n\tWelcome to the XYZ Banking System\n##############################')
    print()
    print('PLEASE ENTER THE NUMBER CORRESPONDING TO YOUR DESIRED COMMAND IN THE PROMPT BELOW : \n\t1.ACCESS CUSTOMER DETAILS\n\t2.ACCESS TRANSACTION PORTAL\n##############################')
    print()
    var_UserInput=input('>>>')
        if var_UserInput=='1':
            return CustomerPortal()

def CustomerPortal():
    cursor.tables()
    rows = cursor.fetchall()
    for row in rows:
        print (row.customer)

MainMenu()

试试这个。我做了一些改变:

  • 将连接字符串移动到函数中
  • 修改代码,使其更接近PEP-8
  • 固定压痕
这是密码

import pyodbc


def main_menu():
    print('##############################\n\tWelcome to the XYZ Banking System\n##############################')
    print()
    print('PLEASE ENTER THE NUMBER CORRESPONDING TO YOUR DESIRED COMMAND IN THE PROMPT BELOW : \n\t1.ACCESS CUSTOMER DETAILS\n\t2.ACCESS TRANSACTION PORTAL\n##############################')
    print()
    var_user_input=input('>>>')
        if var_user_input=='1':
            return customer_portal()

def customer_portal():
    cnxn = pyodbc.connect('Driver={SQL Server};'
                                'Server=***;'
                                'Database=***;'
                                'uid=***;pwd=***')
    cursor = cnxn.cursor()
    cursor.tables()
    rows = cursor.fetchall()
    for row in rows:
        print (row.customer)
    cursor.close()

if __name__ == "__main__":
    main_menu()

祝你好运

试试这个。我做了一些改变:

  • 将连接字符串移动到函数中
  • 修改代码,使其更接近PEP-8
  • 固定压痕
这是密码

import pyodbc


def main_menu():
    print('##############################\n\tWelcome to the XYZ Banking System\n##############################')
    print()
    print('PLEASE ENTER THE NUMBER CORRESPONDING TO YOUR DESIRED COMMAND IN THE PROMPT BELOW : \n\t1.ACCESS CUSTOMER DETAILS\n\t2.ACCESS TRANSACTION PORTAL\n##############################')
    print()
    var_user_input=input('>>>')
        if var_user_input=='1':
            return customer_portal()

def customer_portal():
    cnxn = pyodbc.connect('Driver={SQL Server};'
                                'Server=***;'
                                'Database=***;'
                                'uid=***;pwd=***')
    cursor = cnxn.cursor()
    cursor.tables()
    rows = cursor.fetchall()
    for row in rows:
        print (row.customer)
    cursor.close()

if __name__ == "__main__":
    main_menu()

祝你好运

你的缩进搞乱了。那代码根本不会运行;它将产生一个
缩进错误
。你是在上面发布专有代码吗?@madpysicast一点也不。谢谢你的关心,你的缩进搞错了。那代码根本不会运行;它将产生一个
缩进错误
。你是在上面发布专有代码吗?@madpysicast一点也不。感谢您的关注。非常感谢您的反馈。这确实是一个缩进问题。另外,我还做了一些其他修改,比如创建一个名为Menu的类,以及使用该类构造函数创建其他菜单。现在一切似乎都井然有序了:-)太好了!你能把答案标对吗?听起来你走对了路!非常感谢您的反馈。这确实是一个缩进问题。另外,我还做了一些其他修改,比如创建一个名为Menu的类,以及使用该类构造函数创建其他菜单。现在一切似乎都井然有序了:-)太好了!你能把答案标对吗?听起来你走对了路!