Python倒计时游戏需要指导

Python倒计时游戏需要指导,python,Python,我正在创建一个python倒计时程序,但遇到了问题 这是我的密码: import time def countdown(count): while (count >= 0): print ("Time remaining: "+ str(count) + " seconds") count -= 1 time.sleep(1) countdown(120) print("Times up!") time.sleep(3) 我得到

我正在创建一个python倒计时程序,但遇到了问题

这是我的密码:

import time

def countdown(count):
    while (count >= 0):
        print ("Time remaining: "+ str(count) + " seconds")
        count -= 1
        time.sleep(1)

countdown(120)
print("Times up!")
time.sleep(3)
我得到的结果是:

Time remaining: 120 seconds
Time remaining: 119 seconds
Time remaining: 118 seconds
Time remaining: 117 seconds
Time remaining: 116 seconds
我想将程序更改为分钟和秒,以使程序输出:

You have 2 minutes and 2 seconds remaining.
You have 2 minutes and 1 seconds remaining.
You have 2 minutes and 0 seconds remaining.
You have 1 minutes and 59 seconds remaining.
ect


如何转换?

将打印时间的行更改为:

print("You have {} minutes and {} seconds remaining.".format(*divmod(count, 60)))
以下是完整的脚本:

import time

def countdown(count):
    while (count >= 0):
        print("You have {} minutes and {} seconds remaining.".format(*divmod(count, 60)))
        count -= 1
        time.sleep(1)

print("Welcome. This program will put your computer to sleep in 5 minutes.")
print("To abort shutdown, please close the program.\n") 

countdown(120)
print("Times up!")
time.sleep(3)
和一个示例运行:

Welcome. This program will put your computer to sleep in 5 minutes.
To abort shutdown, please close the program.

You have 2 minutes and 0 seconds remaining.
You have 1 minutes and 59 seconds remaining.
You have 1 minutes and 58 seconds remaining.
You have 1 minutes and 57 seconds remaining.
You have 1 minutes and 56 seconds remaining.
You have 1 minutes and 55 seconds remaining.
...

最后,这里有一个参考on和一个on。

将打印时间的行更改为:

print("You have {} minutes and {} seconds remaining.".format(*divmod(count, 60)))
以下是完整的脚本:

import time

def countdown(count):
    while (count >= 0):
        print("You have {} minutes and {} seconds remaining.".format(*divmod(count, 60)))
        count -= 1
        time.sleep(1)

print("Welcome. This program will put your computer to sleep in 5 minutes.")
print("To abort shutdown, please close the program.\n") 

countdown(120)
print("Times up!")
time.sleep(3)
和一个示例运行:

Welcome. This program will put your computer to sleep in 5 minutes.
To abort shutdown, please close the program.

You have 2 minutes and 0 seconds remaining.
You have 1 minutes and 59 seconds remaining.
You have 1 minutes and 58 seconds remaining.
You have 1 minutes and 57 seconds remaining.
You have 1 minutes and 56 seconds remaining.
You have 1 minutes and 55 seconds remaining.
...

最后,这里有一个打开的引用和一个打开的引用。

每次迭代睡眠1秒,所以count是剩余的秒数

分钟数为计数/60,剩余秒数为计数%60模。所以你可以写一些像

mins = count / 60
secs = count % 60

print "Time remaining is %d minutes %d seconds" % (mins, secs)
您可以在一次操作中计算分钟和秒数,分钟,秒=divmodcount,60

请注意睡眠是不精确的;它所承诺的是,您的程序将睡眠不少于指定的数量。您会注意到,有时程序的暂停时间比挂钟的暂停时间短几秒钟


如果您想要更高的精度,您应该计算循环结束的最后时间,检查每次迭代的当前时间,并显示它们之间的实际差异。

每次迭代睡眠1秒,因此count是剩余的秒数

分钟数为计数/60,剩余秒数为计数%60模。所以你可以写一些像

mins = count / 60
secs = count % 60

print "Time remaining is %d minutes %d seconds" % (mins, secs)
您可以在一次操作中计算分钟和秒数,分钟,秒=divmodcount,60

请注意睡眠是不精确的;它所承诺的是,您的程序将睡眠不少于指定的数量。您会注意到,有时程序的暂停时间比挂钟的暂停时间短几秒钟


如果您想要更高的精度,您应该计算循环结束的最后时间,检查每次迭代的当前时间,并显示它们之间的实际差异。

当您按原样运行此程序时,您能告诉我们您的输出是什么吗?@jwarner112:我已编辑了我的问题,以包括我的输出。请看一看并提前感谢。@user3088253看一看divmod内置函数Print您还有+strintcount/60+分钟和+strcount%60+秒剩余时间,当您按原样运行此程序时,您能告诉我们您的输出是什么吗?@jwarner112:我已编辑了我的问题以包含我的输出。请提前查看并感谢。@user3088253查看divmod内置函数print您还有+strintcount/60+分钟和+strcount%60+秒