为什么我的代码不让我打印它运行了多少次?

问题描述

所以,我知道这个问题令人困惑,但请听我说。我试图计算我的代码运行了多少次,我得到了,但是当我尝试将它实现到一行打印代码中时,它告诉我它无法打印整数。为什么?

    count = 0
    Now = datetime.datetime.Now()
    Current_time = Now.strftime("%H:%M")
    start = datetime.time(10)
    end = datetime.time(11)
    while start <= Now.time() <= end:
        count+=1
        print("This alarm has gone off + count + "times")
        winsound.PlaySound("C:/Users/gabec/Documents/Audacity/Untitled.wav",winsound.SND_ASYNC)
        time.sleep(14) ##The sound is 14 seconds long.

解决方法

您也可以考虑使用 f 字符串:

print(f"This alarm has gone off {count} times")

,

您使用的语法不正确。试试改成这样:

print("This alarm has gone off",count,"times")

即使您为字符串添加了缺失的双引号,也不能添加带有整数 ("This alarm....") 的字符串 (count)。

,

您的报价不正确:'“此警报已关闭” + 计数 + “次数”' 此外,您需要通过键入“str(count)”来更改 int 中的计数(可能不是我的问题) 一开始,(另外,我把 int() 放在开头,以防它会重复。)

count = 0
    now = datetime.datetime.now()
    Current_time = now.strftime("%H:%M")
    start = datetime.time(10)
    end = datetime.time(11)
    while start <= now.time() <= end:
        int(count)        
        count+=1
        str(count)
        print("This alarm has gone off + count + "times")
        winsound.PlaySound("C:/Users/gabec/Documents/Audacity/Untitled.wav",winsound.SND_ASYNC)
        time.sleep(14) ##The sound is 14 seconds long.