2016-07-05 9 views
0

私は最初のPythonプロジェクトを一緒にハッキングしています。私は以下のPythonコードを実行しており、linecache.getlineに問題があります。私はそれがstoarage.txtファイルを開くことを望みます。数字が0であることを確認します。そうであれば、1に変更します。音楽を再生し、LEDをオンにします。その後、2に切り替えると、電源を切ってラズベリーパイを起動するたびに音楽が再生されません。どんな提案もすごい素晴らしいだろう。外部の.txtファイルから行を読み取る、ラインキャッシングに関するPythonの問題

マイstorage.txtはこのように終わるさ:

0 

0 

0 

私はそれは次のようになります考えた:

2 
0 
0 

私のPythonのコードは次のようになります。

import requests 
import pygame 
import RPi.GPIO as GPIO 
from BeautifulSoup import BeautifulSoup 
import linecache 


pygame.mixer.init() 
pygame.mixer.music.load('chaching.wav') 


GPIO.setmode(GPIO.BCM) 
#Im using pin 23 for the led. 
GPIO.setup(21, GPIO.OUT) 
#Im using pin 4 as a basic switch in replacement for a motion sensor. 
GPIO.setup(4, GPIO.IN) 

#Cleans up the number taken from my php file. 
Soup = BeautifulSoup 

#Subscriber number 
sub_num = 0 

#goal one value 
goal1 = 93 
#goal1 celebration flag pulled from the txt file. Line 1 of the doc. 
goal1cel = linecache.getline('storage.txt', 1) 
goal2 = 100 
goal2cel = linecache.getline('storage.txt', 2) 
goal3 = 150 
goal3cel= linecache.getline('storage.txt', 3) 

detectDaniel = 0 


while True: 
    response = requests.get('http://www.bringyourownlaptop.com/php-test') 
    html= response.content 

    soup = BeautifulSoup(html) 
    num = soup.find('section') 
    sub_num = int(num.contents[0].strip()) 

    # figure out if goal have been reached and set the appropriate goal celebration flag 
    if sub_num == goal1 and goal1cel == 0: 
     goal1cel = 1 

    if sub_num == goal2 and goal2cel == 0: 
     goal2cel = 1 

    if sub_num == goal3 and goal3cel == 0: 
     goal3cel = 1 

    # This passed the current status of the goal1cel e.g. 0 not done, 1 current, 2 finished. 
    text_file = open("storage.txt", "w") 
    #This added it to the text document with slash n used as a line break. Weve also had to change the value to a string instead of a integer. 
    text_file.write(str(goal1cel)+"\n"+str(goal2cel)+"\n"+str(goal3cel)) 
    #closes the file. 
    text_file.close() 


    detectDaniel = 1 

    # checks if celebrations need to happen from goal celebration flags 
    if goal1cel == 1 and detectDaniel == 1: 
     GPIO.output(21, True) 
     pygame.mixer.music.play() 
     while pygame.mixer.music.get_busy() == True: 
      continue 
     print("Goal 1 reached!") 
     goal1cel = 2 

    if goal2cel == 1 and detectDaniel == 1: 
     GPIO.output(21, True) 
     pygame.mixer.music.play() 
     while pygame.mixer.music.get_busy() == True: 
         continue 
     print("Goal 2 reached!") 
     goal2cel = 2 

    if goal3cel == 1 and detectDaniel == 1: 
     print("Goal 3 reached!") 
     goal3cel = 2 


    print(sub_num) 
    print(detectDaniel) 
+0

'linecache'私は関連モジュールについて知りませんでした。すべての行に" \ n \ r "があります。 1行で1つの文字を検出する(ただし、charは読み込みできません)。 – dsgdfg

答えて

0

あなたの間違いは、linecache.getlineが単に整数を返すと仮定することにあります。インタプリタで実行すると、文字列である文字列'0\n'が返されます。

あなたがラインを手に入れたら、あなたが望む数字を得るための何か方法を見つける必要があります(1文字の値だけになる場合は、最初の文字if goal1cel == '1'のように比較してください。

+0

速い応答に感謝Kevin。あなたの言っていることを理解していますが、どうやって実装するのかまだ分かりません。どんなタイプのコーディングでも、私はこれまでのところスピードを落としてしまっています。時間があれば、どのように書くかも私に見せることができますか?Dan –

+0

文字列の最初の文字は気にする必要があるので、 'if goal1cel [0] == '1''(行の最初の文字が1の場合はtrueを返します) 。文字列や配列を理解していない人は、Pythonチュートリアルを読むことができます。これらのチュートリアルは、言語を使用する上での中心的な概念であり、基本的な知識が必要です。 –

+0

ケビンあなたの助けをありがとう。とても有難い。 –

関連する問題