【问题标题】:counting code giving me 0 instead of a value计数代码给我 0 而不是一个值
【发布时间】:2020-08-02 08:39:05
【问题描述】:

这是我的代码:

from threading import Timer

    timeout = 5
t = Timer(timeout, print, ['Sorry, times up'])
t.start()

count_push1 = + 1
print("Please push the button to count up.You have 5 seconds.")
while True:
   

     if bool(push_button1.read()):
            press_count = + 1
            print(press_count)
            sleep(0.2)
            break
    
        else:
            print('You pressed', press_count, 'times.')
            break
        break

我希望用户有 5 秒。在这 5 秒内,用户将单击一个按钮,计时器将重置为 5 秒。如果在 5 秒内未单击按钮,则显示用户按下的总次数按钮。我试过了,但是当我运行代码时,代码会自动结束。请有人帮忙

【问题讨论】:

  • 这能回答你的问题吗? Resettable Timer object implementation python
  • 不是真的,每次检测到输出时我都需要重置计时器
  • 将您的第一个和最后一个break 替换为给定链接中的timer.reset()

标签: python python-3.x button arduino-uno


【解决方案1】:

您的运算符顺序错误。在您的while循环中,您需要将计数器更改为count_push1 += 1

在您将变量定义为 1 的顶部,为了便于阅读,我将删除 +

编辑:count_push1 += 1count_push1 = count_push1 + 1 的缩写

【讨论】:

  • 哦,好吧,但即使在我的输出为 0 之后
  • 你的 breaks 会立即将你踢出 while 循环
猜你喜欢
  • 1970-01-01
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多