【问题标题】:I want to loop my code in python for Raspberry pi我想在 python 中为 Raspberry pi 循环我的代码
【发布时间】:2015-10-30 12:01:49
【问题描述】:

这是我的代码,我不知道如何循环它。谢谢帮助我!!

#!/usr/bin/python 
import os
os.system('clear')

#Clear Fenetre et import

print "Test programme";

#Splash


Bacvar = raw_input("Code pour Bac:")

Prodvar = raw_input("Code pour Produit:")
code = str(Bacvar)+str(Prodvar)

#Entree de variables

print code
with open("test.txt", "wt") as out_file:
 out_file.write(code + "\n")


#Export vers txt

所以基本上该程序连接到树莓派和条形码扫描仪。用户将扫描一个盒子。我希望将该数据作为“代码”导出到 txt 文件,并在文件导出后,循环到代码的开头。我尝试了while True:,但它似乎不起作用。

因此,当通过扫描项目将代码导出到我的 txt 文件时。生成的第二个代码应该导出到相同的 txt 但在第二行等等。

谢谢!

【问题讨论】:

  • 当你使用while true时会发生什么?

标签: python loops


【解决方案1】:

诀窍是使用 exception 和事件 KeyboardInterrupt 例如。

try:
    while True:
        #your code
except KeyboardInterrupt:
    break

当您按下键盘键时,您将退出无限循环。现在它可以在带有 I/O 输入的 Raspberry Pi 上工作吗……它应该让我们知道 ^^

【讨论】:

    【解决方案2】:

    我试过了:

    import os
    

    os.system('clear')

    清除 Fenetre 和导入

    打印“测试程序”;

    飞溅

    当真时:

        Bacvar = raw_input("Code pour Bac:")
        Prodvar = raw_input("Code pour Produit:")
        code = str(Bacvar)+str(Prodvar)
        print code
    
        with open('export.txt','w') as f:
        f.write(code)
    

    除了键盘中断: 打破

    当我执行它时,我得到了这个: pi@raspberrypi ~ $ ./test3.py 文件“./test3.py”,第 21 行 f.write(代码) ^ IndentationError:需要一个缩进块 pi@raspberrypi ~ $

    感谢帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2016-07-23
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多