为什么“bool”对象不可调用?

2024-09-29 23:25:48 发布

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

所以,我试图用Python创建一个脚本来自动打开在线类。 这是我的代码:

import webbrowser
import datetime
import time

now = time.strftime("%D, %H:%M")

lesson1 = "03/09/21, 14:10"
lesson2 = "03/10/21, 14:11"
lesson3 = "03/10/21, 14:12"

while True (now != lesson1 and now != lesson2 and now != lesson3):
    print ("Waiting, the current time is " + now)
    now = time.strftime("%D, %H:%M")
    time.sleep(1)
    if (now == lesson1):
        print ("LESSON IS OPENING :D")
        webbrowser.open("https://google.com")
        while (now != "12:00"):
            time.sleep()

    if (now == lesson2):
        print ("LESSON IS OPENING :D")
        webbrowser.open("https://google.com")

    if (now == lesson3):
        print ("LESSON IS OPENING :D")
        webbrowser.open("https://google.com")

当我尝试运行脚本时,会收到以下消息:

Traceback (most recent call last):
  File "/home/matteo/Desktop/Python Project/automatic.py", line 11, in <module>
    while True (now != lesson1 and now != lesson2 and now != lesson3):
TypeError: 'bool' object is not callable
[Finished in 0.053s]

有人能帮我吗


Tags: andimportiftimeisopennowprint
1条回答
网友
1楼 · 发布于 2024-09-29 23:25:48

我认为你想要的条件相当好

while now != lesson1 and now != lesson2 and now != lesson3:

True (now != lesson1 and now != lesson2 and now != lesson3)被视为对True的函数调用,该函数不是可调用的,而是布尔值

另外,我认为您可能希望将三个if移动到while循环之后,这样,只有在while终止时,才会在三个可能的值之间检查时间

相关问题 更多 >

    热门问题