gpt4 book ai didi

Python函数缩进错误: unexpected indent

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:09 27 4
gpt4 key购买 nike

我的代码中存在缩进错误问题。它看起来是正确的...有人能指出我做错了什么吗?我的查询行不断收到错误。

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()

最佳答案

一个函数中不能有多个 return 语句。

相反,您可能想返回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

或者,您可以创建一个 namedtuple()除了位置查找之外,还可以进行属性查找:

import collections

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

另请参阅:

关于Python函数缩进错误: unexpected indent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com