開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
退屈なことはPythonにやらせよう ―ノンプログラマーにもできる自動化処理プログラミング (Al Sweigart(著)、相川 愛三(翻訳)、オライリージャパン)の第Ⅱ部(処理の自動化)、8章(ファイルの読み書き)、8.10(演習プロジェクト)、8.10.2(作文ジェネレーター)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 words = ['ADJECTIVE', 'NOUN', 'ADVERB', 'VERB'] with open('input2.txt') as in_fh, \ open('output2.txt', 'w') as out_fh: text = in_fh.read() for word in words: w = input(f'Enter a {word.lower()}:\n') text = text.replace(word, w) print(text, file=out_fh, end='') print(text, end='')
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py sample2.py Enter a adjective: silly Enter a noun: chandelier Enter a adverb: screamed Enter a verb: pickup truck The silly panda walked to the chandelier and then pickup truck. A nearby chandelier was unaffected by these events. C:\Users\...>type input2.txt The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events. C:\Users\...>type output2.txt The silly panda walked to the chandelier and then pickup truck. A nearby chandelier was unaffected by these events. C:\Users\...>
0 コメント:
コメントを投稿