開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Python Crash Course (Eric Matthes (著)、No Starch Press)のPART1(BASICS)、Chapter 10.(FILES AND EXCEPTIONS)のTRY IT YOURSELF 10-3、4、5(No.5599)を取り組んでみる。
TRY IT YOURSELF 10-3、4、5(No.5599)
コード(Emacs)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- print('10-3.') with open('guest.txt', 'w') as f: name = input() print(name, file=f) print('10-4.') with open('guest_book.txt', 'w') as f: while True: name = input() if name == 'q': break print('hello, {0}!'.format(name)) print(name, file=f) print('10-5.') while True: reason = input() if reason == 'q': break with open('reasons.txt', 'a') as f: print(reason, file=f)
入出力結果(Terminal, IPython)
$ ./sample3.py 10-3. name0 10-4. name1 hello, name1! name2 hello, name2! q 10-5. reason1 reason2 q $ cat guest.txt name0 $ cat guest_book.txt name1 name2 $ cat reasons.txt reason1 reason2 $
0 コメント:
コメントを投稿