【问题标题】:Why 'bool' object is not callable?为什么'bool'对象不可调用?
【发布时间】:2021-06-07 09:23:36
【问题描述】:

所以,我正在尝试使用 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]

谁能帮我解决这个问题?

【问题讨论】:

  • True (now != lesson1 and now != lesson2 and now != lesson3) 被认为是调用带有括号中的参数的方法 True 所以这就是你得到错误的原因。你想添加什么作为条件?
  • 你有while True (...)。我想你想删除True

标签: python loops while-loop boolean boolean-operations


【解决方案1】:

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

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

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

另外,我认为您可能希望在while 循环之后移动三个ifs,以便仅在while 终止时检查三个可能值之间的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2019-09-22
    • 2021-12-15
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多