【问题标题】:How to make while loop keep going while still looking for input in Python如何在 Python 中寻找输入的同时让 while 循环继续运行
【发布时间】:2015-10-15 19:29:51
【问题描述】:

我正在用 Python 制作一个类似游戏的 Cookie 点击器,我试图让 while 循环继续运行,以便它检查一秒钟是否已经过去(因此它添加“cookies”),同时仍在查看用户是否正在制作输入(点击“Cookie”)。

每当我尝试此操作时,它都会等待输入,而不是执行其余的循环。

这是我当前代码的样子(当然还有更多):

While True:
    console.clear()
    cookieText = "You have " + str(cookies) + " cookies"
    print cookieText
    clicker = raw_input("")
    if clicker == "":
        cookies += clickerPower
    more...

这是我要补充的:

While True:
    last = time()
    if last == timer:
        timer = last
        more...

【问题讨论】:

标签: python while-loop user-input


【解决方案1】:

这对我有用 (source)

import msvcrt

num = 0
done = False
while not done:
    print num
    num += 1

    if msvcrt.kbhit():
        print msvcrt.getch()
        done = True

【讨论】:

    【解决方案2】:

    答案是threading。但这并不简单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-26
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多