我正在获取NameError:用于以下代码...

d = [('1','as'),('2','sd')]
for i in d:
    RD = ReleaseDeal(int(i[0]))
    print(RD)

def ReleaseDeal(a):
    RD = '''<ReleaseDeal><DealReleaseReference>R'''+ no +'''</DealReleaseReference><Deal><DealTerms><CommercialModelType>AsPerContract</CommercialModelType>
        <Usage><UseType UserDefinedValue="GoogleMusicBasic">UserDefined</UseType> <UseType UserDefinedValue="SscSnmLocker">UserDefined</UseType>
        <UseType UserDefinedValue="GoogleMusicSubscription">UserDefined</UseType></Usage><TerritoryCode>Worldwide</TerritoryCode><PriceInformation>
        <PriceType Namespace="DPID:"">13</PriceType></PriceInformation><ValidityPeriod><StartDate>2018-10-04</StartDate></ValidityPeriod>
        </DealTerms></Deal></ReleaseDeal>'''
    return RD


我遇到以下错误...

Traceback (most recent call last):
  File "example.py", line 3, in <module>
  RD = ReleaseDeal(int(i[0]))
NameError: name 'ReleaseDeal' is not defined


请帮助我,在此先谢谢..

最佳答案

您遇到了几个错误:


在引用之前定义一些东西
该参数不适用于ReleaseDeal中使用的参数
将int连接到字符串失败。

def ReleaseDeal(no): # this was a, is has to be no and string
    RD = '''<ReleaseDeal><DealReleaseReference>R'''+ no +'''</DealReleaseReference><Deal><DealTerms><CommercialModelType>AsPerContract</CommercialModelType>
    <Usage><UseType UserDefinedValue="GoogleMusicBasic">UserDefined</UseType> <UseType UserDefinedValue="SscSnmLocker">UserDefined</UseType>
    <UseType UserDefinedValue="GoogleMusicSubscription">UserDefined</UseType></Usage><TerritoryCode>Worldwide</TerritoryCode><PriceInformation>
    <PriceType Namespace="DPID:"">13</PriceType></PriceInformation><ValidityPeriod><StartDate>2018-10-04</StartDate></ValidityPeriod>
    </DealTerms></Deal></ReleaseDeal>'''
    return RD

d = [('1','as'),('2','sd')]
for i in d:
    RD = ReleaseDeal(i[0])
    print(RD)



也许类型提示对您有用;-) https://docs.python.org/3/library/typing.html#typing.ClassVar然后您可以说类似

ReleaseDeal(no: str) -> str:


因此,您要获取任何类型的字符串并返回字符串。

关于python - NameError:名称“ReleaseDeal”未在Django中定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53169310/

10-10 18:21