[英]Why doesn't my code let me print how many times it's run?
So, I know the question is confusing, but hear me out.所以,我知道这个问题令人困惑,但请听我说完。 I'm trying to count how many times my code has run, and I got that, but when I try and implement that into a line of printing code, it tells me it can't print integers.我试图计算我的代码运行了多少次,我得到了,但是当我尝试将它实现到一行打印代码中时,它告诉我它不能打印整数。 Why?为什么?
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.
You can also consider using an f-string:您还可以考虑使用 f 字符串:
print(f"This alarm has gone off {count} times")
You're using incorrect syntax.您使用的语法不正确。 Try changing into this:试试改成这样:
print("This alarm has gone off", count, "times")
Even if you include your missing double quote for the string, you cannot add a string ( "This alarm...."
) with an integer ( count
).即使您为字符串添加了缺少的双引号,也不能使用 integer ( count
) 添加字符串 ( "This alarm...."
)。
Your quotes are incorrect: '"this alarm as gone off" + count + "times"' also, you need to change count from a int (probably not my problem) by typing 'str(count)' at the start so, (also, I put int()
at the start just in case it will repeat.)您的引号不正确:'“此警报已关闭”+ 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.