Python 3.x 将对象属性与字符串进行比较赢得';行不通

Python 3.x 将对象属性与字符串进行比较赢得';行不通,python-3.x,Python 3.x,我试图搜索一个对象的排序列表,并将用户输入与这些对象的名称值进行比较,但它会说正在比较的名称不存在于列表中的一个对象中 到目前为止,我已经尝试使用 for network in foundNetworks: if network.name == userInputName: print("found!") break 及 我用于排序和创建网络的代码: class Network(): # init function must be like th

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

到目前为止,我已经尝试使用

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

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

    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()
输出 预期产量
networks.txt
文件包含几个网络名称的尾随空格,例如(为强调添加引号):

代码会费劲地删除每行末尾的换行符,但最好去掉所有尾随的空格。您可以使用:

networkName = networkName.rstrip()

这将删除多余的空格和换行符,应该可以解决问题。

正确缩进代码。缩进在Python中至关重要。请提供一个具体的列表,其中您声称要搜索的名称无法以可复制的方式找到。请查看是否可以仅使用字符串列表来复制该列表。我把美元赌在甜甜圈上,你期待一个不区分大小写的比较,但如果你不提供任何实际相关的信息,我帮不了你。你是如何获得用户输入的?用
print(network.name)
替换成
print(repr(network.name))
。我打赌这会把事情弄清楚的`它仍然说没有找到网络。您认为这可能是其他问题吗?您是否修改了代码或输入文件。一旦我在每个输入行上调用
rstrip()
,它就对我有效,除非输入有什么不同,否则它也应该对你有效。你的权利!我不小心修改了一些东西。感谢您在这方面帮助我:D@Inviss:不用担心。
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 >> 
"fbiSurveillance                       "
"SBEGA 42932    "
"mi network                        "
"unhakavle                       "
networkName = networkName.rstrip()