如何使嵌套的if语句正常工作?

2024-04-20 09:00:26 发布

您现在位置:Python中文网/ 问答频道 /正文

当joinedarea触发时,不输出“person joined area!”它循环回到if语句,检查trade是否为none。我试图将它嵌套在elif语句中,但我不确定语法哪里出了问题。你知道吗

def tradescan(d):
    logfile = open("log.txt","r")
    loglines = follow(logfile)
    for line in loglines:
        trade = re.match("([0-9]{4}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*\\[INFO Client [0-9]*] @(To|From) (?:<(.+)> )?([a-zA-Z_, ]+): (.+)", line)
        joinedarea = re.match("([0-9]{4}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*\\[INFO Client [0-9]*] : (\\S+) has joined the area\\.", line)
        #print(joinedarea[2])
        incoming = "From"
        if trade is None:
            print("Scanning game for appropriate trades...")
        elif (trade[2] == incoming):
            user = trade[4]
            splitat = 25
            r = trade[5][splitat:]
            split = r.split()
            int_list = re.findall("[0-9]+", r)
            try:
                price1 = int_list[0]
            except IndexError:
                print("Trade Mismatch - ignoring trade from", user)
                continue

            try:
                price2 = int_list[1]
            except IndexError:
                print("Trade Mismatch - ignoring trade from", user)
                continue

            #print(price1, price2)
            matches = {x for x in d if x in r}
            items = list(matches)
            try:
                iname1 = items[0]
            except IndexError:
                print("Trade Mismatch - ignoring trade from", user)
                continue

            try:
                iname2 = items[1]
            except IndexError:
                print("Trade Mismatch - ignoring trade from", user)
                continue

            print(Fore.YELLOW + Back.MAGENTA + Style.BRIGHT + user, "wants", price1, iname1, "for", price2, iname2 + Style.RESET_ALL),
            py.moveTo(567, 26, 2)
            py.click()
            py.typewrite(['enter'])
            py.typewrite('/invite ' + user + '\n')
            try:
                userjoin = joinedarea[2]
            except TypeError:
                print("Waiting for", user, "to join area...")
                continue
            if joinedarea:
                print(userjoin, "joined area!")

Tags: forifarealistprinttrytradeexcept