首页 > 解决方案 > 如何继续使用 IF 并使用 Python3

问题描述

在这两种情况下我都需要继续。如果为假,则继续,如果为真,则运行 GetSess... 并继续。

下面的代码如果 UNKNOWN,执行并在 else: 处停止。它不循环。尝试了很多方法来修复,但仍然无法正常工作。

(已编辑)

if gsid.find("OverAllDescription").text == 'UNKNOWN':
    GetSessId = '<?xml version="1.0" encoding="UTF-8"?> <OSS xmlns="http://www.zhone.com/OSSXML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zhone.com/OSSXML ossxml.xsd"> <Request> <RequestType>authenticate</RequestType> </Request> <RequestElement> <Attribute> <Name>loginName</Name> <Value>zmstest</Value> </Attribute> <Attribute> <Name>password</Name> <Value>zmstest</Value> </Attribute> </RequestElement> </OSS>\n' 
    s.sendall(GetSessId.encode('utf-8'))
    response = s.recv(10240)
    print(response)
    gsidr = ET.fromstring(response)
    sessid = gsidr.findall("ResponseElement/SessionID")[0].text
    #print(sessid)
else:


code

我需要类似的东西:

如果 gsid != 'UNKNOWN'continue 并且如果 gsid == 'UNKNOWN',运行 GetSess... 并继续。

标签: python-3.x

解决方案


 if gsid.find("OverAllDescription").text != 'UNKNOWN':
     continue
else:
GetSessId = '<?xml version="1.0" encoding="UTF-8"?> <OSS xmlns="http://www.zhone.com/OSSXML" xmlns:xsi`enter code here`="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zhone.com/OSSXML ossxml.xsd"> <Request> <RequestType>authenticate</RequestType> </Request> <RequestElement> <Attribute> <Name>loginName</Name> <Value>zmstest</Value> </Attribute> <Attribute> <Name>password</Name> <Value>zmstest</Value> </Attribute> </RequestElement> </OSS>\n' 
            s.sendall(GetSessId.encode('utf-8'))
            response = s.recv(10240)
            #print(response)
            gsidr = ET.fromstring(response)
            sessid = gsidr.findall("ResponseElement/SessionID")[0].text
            #print(sessid)

推荐阅读