Python-比较数字。我正在将1与1比较,但失败了-请查看代码-有人可以告诉我这是什么问题吗?

时间:2018-08-15 18:00:57

标签: python relational-operators

if __name__ == '__main__':
    strSize = input()
    inpt  = input()

    strArrSize = strSize.split()
    if (len(strArrSize) ==2):
        n = strArrSize[0]
        m = strArrSize[1]

        inptArray = inpt.split()

        print(n)
        print(len(inptArray))

        if (n == len(inptArray)):
            print("true")
        else:
            print("Given " + str(n) + " integers but entered " + str(len(inptArray)))
    else:
        print("Invalid itnput. The first line must have two space seperated integers.")

这是我得到的输入和输出。         

PS S:\ Study \ CH1> python tt.py

        

1 2

        

1

        

1

        

1

        

给出1个整数但输入1个

3 个答案:

答案 0 :(得分:2)

比较token is null object时,它们并不相等,因为{ "list": { "item": [ { "uri": "https://MyFirm.autodeskplm360.net/api/rest/v1/workspaces/921/items/258", "id": 258, "description": "ID000001 - BMW - 2342152", "details": { "descriptor": "ID000001 - BMW - 2342152", "workspaceID": 921, "deleted": false, "versionID": 145, "version": 1, "timeStamp": "2018-01-29T15:22:44.399-05:00", "lastModified": "2018-02-22T08:11:44.263-05:00", "createdByUser": { "uri": "https://MyFirm.autodeskplm360.net/api/rest/v1/users/IvSYN", "id": "IvSYN", "userNumber": 0 }, "owner": { "uri": "https://MyFirm.autodeskplm360.net/api/rest/v1/users/IvSYN", "id": "IvSYN", "userNumber": 0 }, "additionalOwners": { "group": null, "user": null }, "lifecycleState": { "stateID": 0, "stateName": "Work In Progress", "effectivity": false }, "lifecycleStatus": "Working", "latest": true, "working": true, "workflowState": { "stateId": 457, "stateName": "Form Distributed" }, "lastModifiedBy": { "uri": "https://MyFirm.autodeskplm360.net/api/rest/v1/users/IvSYN", "id": "IvSYN", "userNumber": 0 }, "status": "Unreleased", "dmsID": 258 }, "metaFields": { "entry": [ { "key": "AFFECTED_PART_NUMBERS", "fieldData": { "value": "234\2453\4646\2324", "formattedValue": "234\2453\4646\2324", "dataType": "Paragraph" } }, { "key": "AFFECTED_PLANTS_SYSTEM2", "fieldData": { "value": "MO", "formattedValue": "MO", "dataType": "Paragraph" } }, { "key": "SUPPLIER_PCN_NUMBER", "fieldData": { "value": "2342152", "formattedValue": "2342152", "dataType": "Single Line Text" } }, { "key": "AFFECTED_CUSTOMERS_SYSTEM0", "fieldData": { "value": "na", "formattedValue": "na", "dataType": "Paragraph" } }, { "key": "SUPPLIER", "fieldData": { "value": "BMW", "formattedValue": "BMW", "dataType": "Single Line Text" } }, { "key": "AFFECTED_PLANTS_SYSTEM0", "fieldData": { "value": "No access", "formattedValue": "No access", "dataType": "Paragraph" } }, { "key": "PRODUCT_DESCRIPTION", "fieldData": { "value": "SYSTEM FAILTURE", "formattedValue": "SYSTEM FAILTURE", "dataType": "Single Line Text" } }, { "key": "IMPACT", "fieldData": { "value": "3", "label": "PPAP", "dataType": "Radio Button" } }, { "key": "AFFECTED_PLANTS_SYSTEM1", "fieldData": { "value": "HT", "formattedValue": "HT", "dataType": "Paragraph" } }, { "key": "DATE_OF_PCN", "fieldData": { "value": "2018-10-09 00:00:00.0", "formattedValue": "10/09/2018", "dataType": "Date" } }, { "key": "NOTE", "fieldData": { "value": "None", "formattedValue": "None", "dataType": "Paragraph" } }, { "key": "AFFECTED_CUSTOMERS_SYSTEM2", "fieldData": { "value": "BWM (MA)", "formattedValue": "BWM (MA)", "dataType": "Paragraph" } }, { "key": "AFFECTED_CUSTOMERS_SYSTEM1", "fieldData": { "value": "na", "formattedValue": "na", "dataType": "Paragraph" } }, { "key": "ID", "fieldData": { "value": "ID000001", "formattedValue": "ID000001", "dataType": "Auto Number" } }, { "key": "AFFECTED_CUSTOMERS_SYSTEM3", "fieldData": { "value": "AT", "formattedValue": "AT", "dataType": "Paragraph" } } , { "key": "AFFECTED_CUSTOMERS_SYSTEM3", "fieldData": { "value": "HONDA", "formattedValue": "HONDA", "dataType": "Paragraph" } } ] }, "relations": { "entry": [ { "key": "REV0", "value": null }, { "key": "REV1", "value": null } ] } } ] } } n == len(inptArray),而nstr

设置len(inptArray)int时,请将它们转换为n类型。

m

答案 1 :(得分:1)

让我们通过检查值进行调试:

    print('|'+n+'|', type(n))
    print(len(inptArray))

输出:

|1| <class 'str'>
1

input返回字符串。拆分时,您会得到一个较小的字符串。因此,n是一个字符串值。 len返回一个整数。由于这些是不同的基本类型,因此它们不能相等。

使用n = int(strArrSize[0])修复此问题。您可能会想对m做同样的事情。

答案 2 :(得分:0)

您要确保您正在比较like和like。比较'1' == 1会得到False,因为您正在将strint进行比较。

这是一种纠正方法:

strSize = input()
inpt  = input()

strArrSize = strSize.split()
if (len(strArrSize) ==2):
    n = strArrSize[0]
    m = strArrSize[1]

    inptArray = inpt.split()

    print(n)
    print(len(inptArray))

    if (int(n) == len(inptArray)):
        print("true")
    else:
        print("Given " + str(n) + " integers but entered " + str(len(inptArray)))
else:
    print("Invalid itnput. The first line must have two space seperated integers.")

或者正如其他答案所说,您可以在分配intn时转换为m