简体   繁体   English

Python函数IndentationError:意外缩进

[英]Python Function IndentationError: unexpected indent

I'm having issues with an indent error in my code. 我的代码中出现缩进错误。 It looks correct... can anyone point out what I'm doing wrong? 看起来正确...有人可以指出我做错了吗? I keep getting the error on the line of my query. 我在查询行中不断收到错误。

def invoice_details(myDeliveryID):

    conn = pymssql.connect(myMSSQLserver, myMSSQLuser, myMSSQLpassword, myMSSQLdatabase)
    cursor1 = conn.cursor()

    cursor1.execute('''My Query''' +  "'" + myDeliveryID + "'" + ''' More of my query...''')

    InvoiceDetails = cursor1.fetchone()

    myLocation = "%s" % (InvoiceDetails[0])
    myDate = "%s" % (InvoiceDetails[1])
    myInvoiceNumber = "%s" % (InvoiceDetails[2])
    myAccountNumber = "%s" % (InvoiceDetails[3])

    return myLocation
    return myDate
    return myInvoiceNumber
    return myAccountNumber

    conn.close()

You cannot have multiple return statements in a function. 一个函数中不能有多个return语句。

Instead, you probably meant to return InvoiceDetails (which is a tuple): 相反,您可能打算返回InvoiceDetails (这是一个元组):

def invoice_details(myDeliveryID):
    conn = pymssql.connect(myMSSQLserver, myMSSQLuser, myMSSQLpassword, myMSSQLdatabase)
    cursor1 = conn.cursor()

    cursor1.execute('''My Query''' +  "'" + myDeliveryID + "'" + ''' More of my query...''')

    InvoiceDetails = cursor1.fetchone()

    conn.close()

    return InvoiceDetails

Or, you can make a namedtuple() to also have attribute lookups in addition to positional: 或者,您可以使namedtuple()除了位置之外还具有属性查找:

import collections

invoice = collections.namedtuple('Invoice', ['location', 'date', 'number', 'account_number'])
return invoice(*InvoiceDetails)

Also see: 另请参阅:

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

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