如何在Python中将True变为False时进行更改?

时间:2019-05-12 07:55:18

标签: python python-3.x selenium

当我想到“什么?我该怎么办?”时,我试图创建一个旋转斜杠this。关于此代码。

def stat():
    if True:
        driver.get(webs)
        return False


def spin():
    def spinning_cursor():
        while True:
            for cursor in '|/-\\':
                yield cursor

    spinner = spinning_cursor()
    while stat():
        sys.stdout.write(next(spinner))
        sys.stdout.flush()
        time.sleep(0.1)
        sys.stdout.write('\b')

def catch(xpath=xpaths, image_name=image_names):
    try:
        print('Initializing Chrome: ', end="")
        global driver
        driver = webdriver.Chrome()
        spin()

在功能spin()中; while stat()我希望它为真,然后在stat()完成后,我希望它返回false,以便spin()停止并且catch()继续

1 个答案:

答案 0 :(得分:1)

也许您可以尝试这样的事情:

def stat():
    if driver.get(webs):
        return False
    else:
        return True


def spin():
    def spinning_cursor():
        while True:
            for cursor in '|/-\\':
                yield cursor

    spinner = spinning_cursor()
    while stat():
        sys.stdout.write(next(spinner))
        sys.stdout.flush()
        time.sleep(0.1)
        sys.stdout.write('\b')

stat()函数是对Python if expression evaluation的更明确的评估。

这意味着stat()函数可以替换为driver.get(webs)表达式中对if的调用:

def spin():
    def spinning_cursor():
        while True:
            for cursor in '|/-\\':
                yield cursor

    spinner = spinning_cursor()
    while driver.get(webs):
        sys.stdout.write(next(spinner))
        sys.stdout.flush()
        time.sleep(0.1)
        sys.stdout.write('\b')