【问题标题】:Implementing a timer in boggle game Python在 Boggle 游戏 Python 中实现计时器
【发布时间】:2014-01-25 14:12:49
【问题描述】:

我正在尝试为 boggle 游戏实现 60 秒计时器,其中当计时器达到 0 时,用户无法再输入任何单词。现在我正在使用计数器来限制输入的单词数量(设置为 20),我想将其更改为 60 秒的计时器,但我不太确定如何操作。 提前致谢!

x = 20
    while x > 0:
        print ""
        EnterWord = raw_input("Enter word here: ")
        print EnterWord
        x = x - 1
        if x == 0:
            print "20 chances have been used."

【问题讨论】:

    标签: python timer while-loop user-input boggle


    【解决方案1】:

    如果你想要一个计时器,请使用time()

    import time
    
    start = time.time()
    
    while True:
        if time.time() >= (start + 60):
            print("Out of time")
            break
    

    【讨论】:

    • 我已经添加了这个,但它没有打印“时间到了!”如果 time.time() == 0?
    • time.time()当前时间,它永远不会是0。测试time.time() >= (start + 60)
    • 嗯,还是不行,不满足条件还是继续程序?难道是 time.time() 不断更新到当前时间,所以它继续无限循环?因为 Python Shell 一直在运行并出现消息“程序仍在运行!您确定要退出吗?”
    • 试试while True: if time.time() > start + 60: break
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2016-11-22
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多