0
0

More than 3 years have passed since last update.

python : ラーメンタイマーの作成(pyttsx3 , time)

Posted at

pythonでカップラーメンの音声タイマーを作成。

カップラーメンの時間経過をいつも感覚で判断していた。
問題なく食べれているが、せっかくなのでプログラミングの勉強を兼ねて作成。

目次

・コード
・結果

コード

コード
#必要なモジュールをimport,pyttsx3の呼び出し
#pyttsx3 : 読み上げ
#time    : 時間計測
import pyttsx3
import time
engine = pyttsx3.init()

#startは準備開始時間(蓋を開けるとか)
#startの20秒後にお湯を入れる設定
#20秒後にお湯を入れる音声アナウンス
start = time.time()
while True:
    oyu_in = time.time()
    if oyu_in-start >= 20:
        print("お湯を入れてください")
        engine.say('お湯を入れてください')
        engine.runAndWait()

        #お湯を入れた時間
        oyu_wait = time.time()

        break

#お湯を入れてからの待機時間
#ラーメンは180秒(3分)に出来上がると設定
#180秒後に食べられますと音声アナウンス
while True:

    #お湯を入れてから経過した時間
    oyu_end = time.time() 

    if oyu_end-oyu_wait >= 180:
        print("食べられます")
        engine.say('食べられます')
        engine.runAndWait()
        break

#残り30秒になったら音声アナウンス
    elif oyu_end-oyu_wait == 150:
        print(int(oyu_end-oyu_wait),"秒経過")
        engine.say('あと30秒です')
        engine.runAndWait()
        continue

#10秒刻みで経過時間を表示
    elif (oyu_wait-oyu_end)%10 == 0:
        print(int(oyu_end-oyu_wait),"秒経過")

結果

スクリーンショット 2020-12-28 10.46.22.png
PCの処理等の問題で残り30秒や10秒間隔の経過時間表示がないときがあった。
ただ、180秒経過は [ 180 >= ] としているため必ず音声アナウンスが出ます。

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0