将Object属性与字符串进行比较不会起作用

时间:2017-12-09 06:27:42

标签: python-3.x

我正在尝试搜索已排序的对象列表,并将用户输入与这些对象的名称值进行比较,但它会说明所比较的名称不存在于列表中的一个对象中。

我到目前为止尝试使用

for network in foundNetworks:
    if network.name == userInputName:
    print("found!")
    break

if any(network.name == networkName for network in foundNetworks):
    break
else:
    print("Invalid network name!")

我用于排序和创建网络的代码:

    class Network():
        # init function must be like this
        def __init__(self, name, security, signalStrength):
            self.name = str(name)
            self.security = str(security)
            self.signalStrength = int(signalStrength)

            # passwords, credit card info etc.
            loot = set()

    # assume multiple new networks are created with varying names which are not the same
    newNetwork = Network(networkName, securityType, networkStrength)
    foundNetworks.append(newNetwork)


    # sort network based on signal strength
    foundNetworks = sorted(foundNetworks, key=lambda network: network.signalStrength, reverse=True)

更新

以下是我实际获取用户输入并验证其存在的方式。

import time
import linecache
from random import randint


def exampleCheck():
    class Network():
        # init function must be like this
        def __init__(self, name, security, signalStrength):
            self.name = str(name)
            self.security = str(security)
            self.signalStrength = int(signalStrength)


    foundNetworks = []
    securityTypes = ["Joe-Level-Encryption", "AES", "WPA2", "TKIP", "None", "Basic Encryption"]

    for i in range(10):
        while True:
            # uses linecache to randomly pick a name from networks.txt
            networkName = linecache.getline("networks.txt", randint(1, 19))
            networkName = networkName.replace('\n','')

            # ensure network name has not already been found before
            if any(network for network in foundNetworks if network.name == networkName):
                continue
            else:
                break

        # set random network strength and security type
        securityType = securityTypes[randint(0, 5)]
        networkStrength = randint(89, 1005)           

        # create a new network and store it in found networks
        newNetwork = Network(networkName, securityType, networkStrength)
        foundNetworks.append(newNetwork)


    # sort network based on signal strength
    foundNetworks = sorted(foundNetworks, key=lambda network: network.signalStrength, reverse=True)

    # display found networks
    for network in foundNetworks:
        print(network.name)

    while True:
        networkName = input("\nNETWORK TO TARGET >> ")
        if any(network.name == networkName for network in foundNetworks):
            print("Found!")
            break
        else:
            print("Invalid network name!")

def main():
    exampleCheck()


if __name__ == "__main__":
    main()

输出

fbiSurveillance                       
cant get me
SBEGA 42932    
ppsadminlogin
notTorenting
notFakeW1f1
ppsWifi
mi network                        
unhakavle                       
coporateDominion

NETWORK TO TARGET >> mi network
Invalid network name!

NETWORK TO TARGET >> 

预期产出

fbiSurveillance                       
cant get me
SBEGA 42932    
ppsadminlogin
notTorenting
notFakeW1f1
ppsWifi
mi network                        
unhakavle                       
coporateDominion

NETWORK TO TARGET >> mi network
Found!

NETWORK TO TARGET >> 

1 个答案:

答案 0 :(得分:1)

"fbiSurveillance " "SBEGA 42932 " "mi network " "unhakavle " 文件包含几个网络名称的尾部空格,例如: (为强调添加了引号):

networkName = networkName.rstrip()

代码在每行末尾删除换行字符时会遇到麻烦,但最好去除所有尾随空格。您可以使用str.rstrip()

^

这将删除额外的空格和换行符,它应该解决问题。