0

我已经在一个项目上工作了大约五分钟,但我遇到了一个错误:

TypeError:“str”对象不可调用。

谁能帮我看看我的错误?

from win10toast import ToastNotifier as tst
import time
#timer with notifications

toaster = tst()
#the below input shows how long the timer will last
span_seconds = input('How many seconds will your timer span through? ')
#loops the time until the seconds are up
i = 0
while i < span_seconds():
    time.sleep(1)
    span_seconds-1
    #determines whether the timer is done
if i == span_seconds:
    toaster.show_toast('Timer is up!')
4

1 回答 1

1

span_seconds是一个字符串,从input. 您不能调用string. 您也不能调用int. 无论如何,您都不想调用它。您只想引用该变量。您可以省略()来执行此操作。

此外,您的线路span_seconds - 1没有任何作用。我猜你正在寻找类似span_seconds = span_seconds - 1(也写为span_seconds -= 1)的东西。即使写得正确,该行也无法完成您的目标,因为span_seconds是 a string,而不是int.

如果你改变

while i < span_seconds():
    span_seconds-1

while i < span_seconds:
    span_seconds -= 1

正如我上面提到的,也改变了

span_seconds = input('How many seconds will your timer span through? ')

span_seconds = int(input('How many seconds will your timer span through? '))

转换span_secondsint,您的代码可能会按照您希望的方式运行。

于 2020-05-20T20:19:39.070 回答