【问题标题】:Why doesn't my code let me print how many times it's run?为什么我的代码不让我打印它运行了多少次?
【发布时间】:2021-01-10 16:38:37
【问题描述】:

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

    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.

【问题讨论】:

  • 请使用完整的错误回溯更新您的问题。
  • 这个够具体了吗?
  • 不,我看不到任何错误回溯。
  • 好的,“错误回溯”是什么意思?你的意思是我的问题是什么?
  • 可以搜索'python full error traceback'

标签: python string printing integer counting


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    即使您在字符串中包含缺少的双引号,您也无法添加带有整数 (count) 的字符串 ("This alarm....")。

    【讨论】:

      【解决方案3】:

      您的引用不正确:'"此警报已关闭" + count + "times"' 此外,您需要通过键入“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.
      
       
      

      【讨论】:

      • 好的,首先感谢您的报价和计数。其次,我会尝试 `str()' 建议。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 2013-10-04
      相关资源
      最近更新 更多